Welcome to   
"The Blog"

Wednesday, April 22, 2009

Posted in Coding

Javascript Validation - Email and Required Fields

This is a validation script that I was working on with another programmer and it came out really well.  I actually use it for pretty much every form.  All you have to do is copy and paste it, place it in the header, or make a new .js file for it.

 

function elmName()
{
var msg = "";
for(i=0; i<document.forms[0].elements.length; i++) //Counts all the elements in the form (inputs, textarea, etc)
{
if( document.forms[0].elements[i].className.indexOf('required') >=0 ) //Finds all the elements in the form that have a class of required
{
if( document.forms[0].elements[i].value == "" ) // IF required class is empty
{
document.forms[0].elements[i].style.background='#FBFAEC'; //style of the required element
document.forms[0].elements[i].style.borderColor='#FF0000'; // style of the required element
msg = "Please fill in the required fields."; // message to display
alert(msg); //alert message

} else {
document.forms[0].elements[i].style.background='#ffffff'; // style of element if it is filled out correctly
document.forms[0].elements[i].style.borderColor='#000000';// style of element if it is filled out correctly
}
}
}
var reg = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"); //Regular Expression for valid email address
var address = document.forms[0].elements["email"].value; // Finds element with the name 'email'
if(address != "" && reg.test(address) == false) {
document.forms[0].elements["email"].style.background='#FBFAEC';
document.forms[0].elements["email"].style.borderColor='#FF0000';
msg = "Please fill out a correct email address.";
alert(msg);
return false;
}
if( msg.length > 0 )
{
return false;
}else{
return true;
}
}

I know, it's beautiful. =)  Actually I'm sure it can use some cleaning up but it works great for what it's used for. Oh and sorry for the code layout, I know there's are better ways to paste the code so that it's colored and lined and blah blah, but until I get to that, this is what you get =P.

NOTE: If you have a another form on the page like a search form or anything else, you can replace the "0" with the actual name of the form, or the number of the form on the page.

All that's left is to add the onsubmit to your form and add the required classes as well.

onsubmit="return elmName();"

and then your done.

I added all the comments to make the code pretty self explanitory but if you have any questions, feel free to ask them.  OR if you have any modifications to the code, that'd be cool too.

Next I'll show you how to tie this in with some cool ajax functions using jquery and also the PHP code to actually send the mail.


 

What do you think of "Javascript Validation - Email and Required Fields"?

There are currently (3) comments.


Enter Your Nickname

Enter A Comment

What is two plus two?

 

Comments:

March
24
Wednesday

uvGzu3 <a href="http://ghhzsqvhhlgv.com/">ghhzsqvhhlgv</a>, [url=http://zslqkhgljeav.com/]zslqkhgljeav[/url], [link=http://kzxchugpyanh.com/]kzxchugpyanh[/link], http://gjfipvzzujtp.com/

Posted by naqbsmab at 4:35 pm

February
25
Thursday

It would be wise to ask your Verizon representative if the phone you are thinking of purchasing this year requires a data plan addon. If your buying online make sure to read the full print and call Verizon if you are unsure about the charges.

Posted by nike tn at 12:51 am

September
24
Thursday

Thank you! It works like a charm. :)

Posted by Betsy at 11:22 am