Subscribe Now!

Enter your email address:

Wednesday, January 19, 2011

Understanding the Difference Between Server-Side and Client-Side Validation


Understanding the Difference Between Server-Side and Client-Side Validation

Many people new to ASP.NET don't know the difference between client-side and server-side validation. You must understand these two different ways of validating the data users input into a Web form.
After the user enters data into a Web form, clicks the Submit button, and sends the form data to the server as a request, you can perform server-side validation on the data. If the data is incorrect or not valid, you can send back a response stating this. If, however, when the user clicks the Submit button, a scripting language that is part of the overall HTML page is initiated to check the validity of the data before it is sent to the server, this is client-side validation.
It was a lot easier to understand the difference between these forms of validation when you coded Active Server Pages 3.0 because, as the programmer, you personally performed almost all data validation. You yourself either programmed it to be client-side or server-side.
When you used server-side validation with ASP 3.0, if something the user entered was wrong, you could repost the form and ask the user to correct the information in that particular field of the form. Sometimes, you carried the correct input from the other fields back to the form page, and populated the fields for the users so they didn't have to re-enter the same information again. Some sites on the Internet don't carry this inputted information back to the form page, and the user is then required to enter all the information into the form a second time. Obviously, this may cause people to leave your site for another.
The bad thing about server-side validation is that it requires trips back and forth to the server. This takes a lot of resources and makes for a slower-paced form for the user. Nothing is more annoying to a user who is on a dial-up connection than clicking the Submit button on the form and then waiting for 20 seconds to find out that they didn't enter their password correctly.
The other option for form validation is to put some client-side JavaScript or VBScript at the top of the ASP page that checks if the information in the fields is correct. This takes care of the problem of making unnecessary trips to the server, but it requires another language to learn and manage. JavaScript is a great language, but takes a lot of time to master, and there are always problems getting your JavaScript code to work on different browsers. Listing 1 shows you an example of using client-side JavaScript to perform form validation.
Listing 1: Client-side JavaScript for form validation
<script language="javascript">
<!--
Function CheckForm(form)
{
  for(var intCtr = 0; intCtr <= (form.elements.length - 5); ++intCtr)
  {
    var temp = form.elements[intCtr];
    if(temp.type == "text" && temp.value == "")
    {
    alert("Please Enter All Information!");
      temp.focus();
      return false;
    }
  }
  return true;
}
//-->
</script>

This sample piece of JavaScript does some validation, but it doesn't check for all the in-formation that you might need on the form you are building. This piece of code determines only whether the user entered anything at all in all five fields within the form. It does not determine whether the user entered an actual e-mail address within the e-mail address text box, whether the user entered a number between two given numbers, or whether the password and the confirm password text boxes match. After awhile, you can see that you need many JavaScript functions to obtain the level of form validation required.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...