Add dynamic Client and Server side validation on ASP.Net pages

Hi folks,

Having a validation on the webpages is a very important since it protects your site from hacking or spam. Because if your site doesn’t have a good validation then anyone can send many request as possible to your site to slow down the site and it may bring your site down. So in this post we’ll look how we can add some validation to the submission form in ASP.Net. Specially when we want to add some dynamic validation depending on the condition. For example, we have to make some field required depending on some condition.

The validation will be handled on both client and server side. It’s because, most of the time we are using jQuery or JavaScript to do the client side validation and these validation can be manipulated/by-passed easily using developer consoles in web browser.  So by providing server side validation we can add extra protection for the field validation. So lets start the programming part 🙂

validation

 

Client side validation

First we have to keep the value of the validation whether ON or OFF. Add the variable called “IsReferenceNoMandatory” inside the Page_Load function of the page code file.

A hidden variable used to keep track of the value and used with jQuery when doing the client validation.

 

Now time to validate the filed using jQuery. add the following script at the end of your page. Here you have to include jQuery and jQuery validation libraries.  The following code takes the required validation condition and the field value to validate the submission form.

 

Server side validation

When we submit the form it’s first check the client side validation and if the client side validation is OK then it’ll fire the sever side validation that specified in the OnServerValidate tag, which is ValidateControlsReferenceNo(). Server side validation are simple since we have all the variable list on one place. So here we’ll check the IsReferenceNoMandatory and the value of ReferenceNumber to decide the validation as follows.

Hope you get some idea about validations and important. Please leave a comment below if you have any doubts. 🙂

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: