150 likes | 252 Views
Session 4. Advanced. Validation Techniques. Session Objectives. Explore the various Validation Controls. Explain code behind. Implement code behind. Validation Controls. Restricts blank field. RequiredFieldValidator. CompareValidator. Compares two fields. Checks for specified range.
E N D
Session 4 Advanced Validation Techniques Building Applications using ASP.NET and C# / Session 4 / 1 of 15
Session Objectives • Explore the various Validation Controls • Explain code behind • Implement code behind Building Applications using ASP.NET and C# / Session 4 / 2 of 15
Validation Controls Restricts blank field RequiredFieldValidator CompareValidator Compares two fields Checks for specified range RangeValidator Checks value with expression RegularExpressionValidator Checks value by client-side or server-side function CustomValidator ValidationSummary Lists validation errors of all controls on the page Building Applications using ASP.NET and C# / Session 4 / 3 of 15
RequiredFieldValidator Inline error message No value is entered <asp:requiredfieldvalidatorcontroltovalidate="userid" display="static" errormessage="You must enter your user id." runat=server> The User Id cannot be left Blank! </asp:requiredfieldvalidator> Building Applications using ASP.NET and C# / Session 4 / 4 of 15
Validation Error Message Dynamic Display Building Applications using ASP.NET and C# / Session 4 / 5 of 15
CompareValidator <asp:comparevalidatorcontroltovalidate="pwd_con" display="static" errormessage="the confirmation password does not match." controltocompare="pwd" type="String" operator="Equal" runat=server> * </asp:comparevalidator> = , < , > , <= , >= , Not Equal String, Integer, DateTime, Currency, Double <asp:comparevalidatorcontroltovalidate="bid" display="static" errormessage="You cannot enter a bid for lesser than $100.“ valuetocompare=100 type="Integer" operator="GreaterThanEqual" runat="server">* </asp:comparevalidator> Comparing with static value, 100 Building Applications using ASP.NET and C# / Session 4 / 6 of 15
RangeValidator <asp:rangevalidatorcontroltovalidate="r3" type="Integer" minimumvalue="1" maximumvalue="99" errormessage="Your age must be in the range of 1-99 yrs" display="static" runat="server" > * </asp:rangevalidator> Specify value of the control for the range Specify name of the control for the range <asp:rangevalidatorcontroltovalidate="r4" type="Integer" minimumcontrol="r1" maximumcontrol="r2" errormessage="Your age must be in the range of 1-99 yrs" display="static" runat="server" > * </asp:rangevalidator> Building Applications using ASP.NET and C# / Session 4 / 7 of 15
Sign Meaning ^ The caret sign ^ specifies that checking starts from here $ The “$” sign specifies that the checking ends here [] Square brackets “[]” checks that the value entered match with any of the characters that are in the square brackets. \w “\w” allows any value to be entered /d{} “/d” specifies that the value entered is a digit and {} specifies the number of occurrences of the specified data type + The + sign indicates that one or more elements to be added to the expression being checked RegularExpressionValidator - 1 Building Applications using ASP.NET and C# / Session 4 / 8 of 15
RegularExpressionValidator - 2 Validate an email id <asp:regularexpressionvalidatorcontroltovalidate="emailid" display="static" validationexpression="^[\w-]+@[\w-]+\.(com|net|org|edu|mil)$" runat=server> Not a valid e-mail address </asp:RegularExpressionValidator> Building Applications using ASP.NET and C# / Session 4 / 9 of 15
CustomValidator Client-side function <asp:customvalidatorrunat="server" controltovalidate="grade" clientvalidationfunction="clval" onservervalidate="serval" display="static"> Wrong value </asp:customvalidator> Building Applications using ASP.NET and C# / Session 4 / 10 of 15
ValidationSummary <asp:validationsummaryid="vs1" headertext="The errors found are: " displaymode="singleparagraph" runat="server"/> </asp:customvalidator> Building Applications using ASP.NET and C# / Session 4 / 11 of 15
Page.IsValid Property <script language="C#" runat="server" > void validate_page(Object Src, EventArgs E){ if (Page.IsValid == true) { lbl.Text = "Page is Valid!";} else { lbl.Text = "Page is not Valid!"; } } </script> Building Applications using ASP.NET and C# / Session 4 / 12 of 15
Uplevel and Downlevel Browsers Downlevel Uplevel Internet Explorer 4 < < = Browsers Browsers HTML 4 HTML 3.2 Client-side validation Server-side validation <%@ Page ClientTarget=DownLevel %> disable client-side validation Building Applications using ASP.NET and C# / Session 4 / 13 of 15
Code Behind - 1 .aspx file Provides the functionality <%@ Page language="C#" Inherits="codeb" %> <html> <script language="C#" runat ="server" > </script> <form runat="server"> <asp:button OnClick="bMe_Click" text="Click me!" id="bMe" runat="server"/><br><br><br><br> <asp:label id="lb1" runat="server"/> </form> </html> Building Applications using ASP.NET and C# / Session 4 / 14 of 15
Code Behind - 2 using System; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public class codeb: Page { public System.Web.UI.WebControls.Label lb1; public System.Web.UI.WebControls.Button bMe; protected void bMe_Click(Object sender, EventArgs e) { lb1.Text = "Clicked!"; } void Main() { } } .CS file BIN Seperated programming code Building Applications using ASP.NET and C# / Session 4 / 15 of 15