ASP.NET validation controls define an important role in validating the user input data. Whenever the user gives the input, it must always be validated before sending it across to various layers of an application. If we get the user input with validation, then chances are that we are sending the wrong data. So, validation is a good idea to do whenever we are taking input from the user.
There are the following two types of validation in ASP.NET:
- Client-Side Validation
- Server-Side Validation
Client-Side Validation
When validation is done on the client browser, then it is known as Client-Side Validation. We use JavaScript to do the Client-Side Validation. For the last ten years, developers are using JavaScript for Client-Side Validation. JavaScript gives us full control over Client-Side Validation. Now, Microsoft is also embracing jQuery. Client-Side Validation is responsive and quick for end users, but not a secure form of validation. Client-Side Validation is faster, typically looks good, often association among messages and input.
Server-Side Validation
When validation occurs on the server, then its known as Server-Side Validation. Server-Side Validation is a secure form of validation. The main advantage of Server-Side Validation is that if the user somehow bypasses the Client-Side Validation, we can still catch the problem on server-side. Server-side provides more security and ensures that no invalid data is processed by the application. Server-Side Validation is done by writing the custom logic for validating all the input.
The following are the Validation Controls in ASP.NET:
- RequiredFieldValidator Control
- CompareValidator Control
- RangeValidator Control
- RegularExpressionValidator Control
- CustomFieldValidator Control
- ValidationSummary
Step 1
Start Visual Studio.
Step 2
Now, we need to create a website using "File" -> "New" -> "Website".
Step 3
Now, select the ASP.NET Empty Website and click on the OK button.
Step 4
Now, add the Web form by right-clicking on the website and provide a name to the Web Form.
I have given the page name as Validator.
Step 5
After the web form is created, we will design the form as shown in the following screenshot.
Step 6
We will write the following code in the Validator.aspx page.
Step 7
Now, we need to execute the web form using the F5 key and will get the following window form.
I have also written the following code for a button click.
- protected void Button1_Click(object sender, EventArgs e)
- {
- Label1.Visible = true;
- Label1.Text = "All Validator working Perfectly!!!!!!";
-
- }
Step 8
Now, check whether the validation is working correctly or not. When we click on the submit button, then the following error can be seen.
The error shows that the textboxes can't be blank.
Step 9
Now, check the email validation on the web form. If we write the wrong email-id, then the error message appears on the form.
Step 10
Now, we need to check the password validation. If we are writing a different password into the password textboxes, then the error message appears on the web form.
Step 11
Now, we need to check the validation of the dropdown list. If we don't select the country name, then the following error message appears: "plz select your country name".
When all the textbooxes have been filled, select the country name. Now, on clicking the submit button, the following message appears.
Summary
In this article, I explained how validation in ASP.NET works.
I hope this helpful for beginners when they want to do validation on the web pages.