Say you have a requirement where you need to multiply two numbers so here you have to make sure that they are numbers.
How to validate?
A simple approach is to use if statements or a series of if statements that would check for data input satisfying a particular condition. By this method we can check for valid ranges of dates, blank entries and a host of other simple error prone issues.
Another way is to take the benefit of validation controls provided by ASP.NET.Validation in ASP.NET can be carried out both on the client side as well as the server side. These validation controls belong to the System.Web.UI.WebControls namespace.
<?xml:namespace prefix = asp /><asp:ValidationName ErrorMessage="DescriptiveText" ControlToValidate="ControlName" runat="server"></asp:ValidationName>
<asp:RequiredFieldValidator ErrorMessage="Enter a value. You cannot leave this field blank” ControlToValidate=" runat="server" ? txtName?>
Types of validation control in ASP.NET
RequiredFieldValidator :This control helps to prevent user from skipping a field that is required.
CompareValidator: This control helps to compare a user’s entry to that of a constant value.
RangeValidator: This control helps to check whether a user’s entry is between specified lower and upper boundaries.
RegularExpressionValidator :This control helps to check whether a user’s entry matches a pattern defined by a regular expression.
CustomValidator :This control helps to check the user’s entry based on the logic that you code yourself.
The set of validation controls described above are going to act on the following controls:
How Validation Controls Work
The working of validation controls can be explained by steps mentioned below:
1)HTML output is sent to client
2)The user enters input
3)Client side script are executed to determine if fields are valid
4)Check if all controls are valid or not
5)If valid then post data else display error message
6)Finally evaluate the validation controls
Custom validation controls
You can also create your own validation control to suit your need. The syntax is similar to the other validation control.
<asp:CustomValidator controltovalidate="phone" runat="server" left? Errormessage="The phone number must be 7 characters”>Summary