• Home
  • About
Blue Orange Green Pink Purple

Form Validation II

Posted in Tutorial. on Tuesday, February 5th, 2008 by admin Tags: AJAX, forms, Javascript, Tutorial
Feb 05

Continuing on from the first part of the tutorial, we are going to continue through the list of form elements in the order we setup. Next we work on the email address.

else if(document.getElementById(“email”).value==” ||
(
document.getElementById(“email”).value!=” &&
!
emailfilter.test(document.getElementById(“email”).value))){
document.getElementById(“error_msg”).childNodes[0].data=
‘Please enter a valid e-mail address.’;
document.getElementById(“error_msg”).style.display = “block”;
document.getElementById(“email”).focus();
return false;
}

Notice how we handle the email value checking first to see if it is empty to trigger an error and then using the logical operator OR (denoted as ||), we then look at another statement. In that statement we use the logical operator AND (denoted as &&) to trigger an error, and if both the email filed is not empty and the emailfilter.test is not valid then we do what is in between the curly brackets again. The contents are the same as what we described in the first statement.

else if(document.getElementById(“phone”).value!=“” &&
!
phonefilter.test(document.getElementById(“phone”).value)){
document.getElementById(“error_msg”).childNodes[0].data =
“Please enter valid phone number”;
document.getElementById(“error_msg”).style.display = “block”;
document.getElementById(“phone”).focus();
return false;
}

The phone is very similar to the email, but nothing is done if no phone number is entered.

else if(document.getElementById(“subject”).
options[document.getElementById("subject").selectedIndex].value==”){
document.getElementById(“error_msg”).childNodes[0].data=
‘Please choose a subject address.’;
document.getElementById(“error_msg”).style.display = “block”;
document.getElementById(“subject”).focus();
return false;
}

The subject is a handled a little differently as it is part of a select element. You will notice that the format in the IF statement changes to read the chosen value. Giving the default a value=”" allows the application to recognize when no subject is selected.

else if(document.getElementById(“content”).value==”){
document.getElementById(“error_msg”).childNodes[0].data=‘Please be sure to enter comments.’;
document.getElementById(“error_msg”).style.display = “block”;
document.getElementById(“content”).focus();
return false;
}
else { return true; }
}
//–>
</script>

The last item for textarea element is similar to what was done for the name element, and that is all followed the last ELSE which will return a true or you can have it point to another function (like we will do for AJAX call in the final example). If set to true, it will just submit the form.

So now that the script is done, the only thing left is providing a place to display the error. The div/span can be placed where ever you want the error to be displayed. The error will only be displayed when triggered by the javascript.

<span id=“error_msg” class=“errormssg”> </span>

The trick here is when the id error_message style is defined. The key will be to make the class display none. The javascript will trigger the error to be visible (by changing display:block;) and then the error can be changed back by simply changing the style for id=”error_msg” display:block.

So lets take a look at the finished project. You can view and copy the finished product here.

Leave a Reply

dingobytes

  • Most Popular
    550 Banned AJAX Apache Coldfusion css directives Directory effect failure form forms image Javascript message mouseover options rotator sendmail Service unavailable setup smtp auth turorial Tutorial validation VirtualHost web site
  • Archives
    • October 2008
    • August 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
  • flickrRSS probably needs to be setup
  • Archives
    • October 2008
    • August 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
  • Search






  • Home
  • About

© Copyright dingobytes. All rights reserved.
Designed by FTL Wordpress Themes brought to you by Smashing Magazine

Back to Top