..


Sponsored Links

Use and management of validation checks

The fundamental goal of ASP.NET and the. NET Framework is to simplify and speed up the work of programmers. For this reason, the framework includes several features that were previously programmers had to manage themselves, exposing themselves to loss of time and potential errors.

A very common scenario is when you navigate to run into sites that include pages in which users find themselves having to add different types of information. For example, to access a specific section of a Web site users may have to enter a username or password, or to receive certain information must provide a valid email address (containing @, domain, etc.).

One who runs a site like this should therefore be able to rely on a logic that ensures that the information entered is valid. Since it is not possible in 100% of users to enter data that is absolutely valid it is necessary to validate the information. For example, certain fields may be mandatory, others may require that the data entered meet a certain format (like the aforementioned email address), and others that the data entered is within a certain range and so on.

Clearly, these checks could be carried out by programmers writing custom code, but ASP.NET provides a simpler and faster: the validation checks. They work in close relation with the standard controls (like textbox) present in the web form and allow us to obtain error messages or warning if users enter nurses who may be incorrect.

ASP.NET includes six validation controls:

  • RequiredFieldValidator: ensures that a field is not empty
  • RangeValidator: ensures that the value in a field is within a certain range
  • RegularExpressionValidator: valid data in a field by comparing their structure with a default template (eg email address)
  • CompareValidator It ensures that the value in a field corresponds to a specific value
  • CustomValidator: Set custom validation functions
  • ValidationSummary: allows you to view a list of all validation errors on a page

All validation controls work the same way. Before you set a standard control on the web page and then there is the validation that you prefer to control where Copar have any error message. The validation controls have a property called ControlToValidate, in which one must set the name of the control that must be validated. Set this property everything works automatically, although of course you need to configure the message to be returned and you can also set several other properties.

The validation controls work on the following server controls: TextBox, ListBox, DropDownList, RadioButtonList, HtmlInputText, HtmlInputFile, HtmlSelect, HtmlTextArea, FileUpload. To better understand that work as usual we do an example.

Open the test site created in the previous lesson or a new creiamone. Add a Web form called Validazione.aspx to host standard controls and the controls are for. Suppose that a page in which a hypothetical user must enter name, age, phone number and password (in two different fields to confirm they are correct). Insert textbox so many as there are fields, its label and a button for sending the information entered

Now insert the validation checks. Let each of the RequiredFieldValidator next to the fields Name, Surname, Age, Phone, Password and Confirm Password ControlToValidate setting the textbox on the respective setting and error messages

Finally insert a ValidationSummary on the page that will allow us to simultaneously display all error messages. It 'can also set the property to the value of that control ShowMessageBox true to also receive a visual indication of the errors. At this point we start the application and if you click the submit button without entering any information of the data we will see something like

If we try to put the information in some fields we will see that some messages (on the fields filled in) will no longer appear

In this example we used only the RequiredFieldValidator but clearly the use of other types of validation controls is similar. As usual, I invite you to consult the official Microsoft to learn more.

Help with Visual Studio ASP.Net
E-Learning
ASP (Advanced) ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €.
ASP.NET (Course) ASP.NET (Course)
Full course for building Web applications from 49 €.
SQL and Database (Course) SQL and Database (Course)
Create and manage relational databases. From 39 €.
Sponsored Links