Web Hosting  

ECommerce  

Payment  
Processing  

Web Sales &  
Traffic Reports  

FAQs  

About Us  



 

  JavaScript Form Validation

You can use JavaScript to make a form more interactive, to validate data the user enters, and to enter data based on other data. JavaScript enables you to control data as it is entered and check it for accuracy before the CGI program receives it, saving time all around.

Here's a working demonstration of the code below.


<SCRIPT language="JavaScript">
<!--
function checkData (){

if (document.forms[0].YourName.value.length ==0){
alert("Please enter Your name.")
return false}
if (document.forms[0].YourColor.value.length <=2){
alert("Please enter Favorite color. It must be a word larger than 2 characters")
return false}
if (document.forms[0].Email.value.length ==0){
alert("Please enter an email address.")
return false}
if (document.forms[0].Email.value.length >0){
i=document.forms[0].Email.value.indexOf("@")
j=document.forms[0].Email.value.indexOf(".",i)
k=document.forms[0].Email.value.indexOf(",")
kk=document.forms[0].Email.value.indexOf(" ")
jj=document.forms[0].Email.value.lastIndexOf(".")+1
len=document.forms[0].Email.value.length

if ((i>0) && (j>(1+1)) && (k==-1) && (kk==-1) &&
(len-jj >=2) && (len-jj<=3)) {
}
else {
alert("Please enter an exact email address.\n" +
document.forms[0].Email.value + " is invalid.")
return false}}
}
//-->
</script>

(form section)

Your Name
<input type=text size=32 maxlength=50 name="YourName"><b><i>At least one character must be entered.</i></b>

Your Favorite Color
<input type=text size=32 maxlength=50 name="YourColor"><b><i>More than two characters must be entered.</i></b>

Email
<input type=text size=32 maxlength=50 name="Email"><b><i>A correctly formatted email address must be entered.</i></b>


Here's how it works:

The values in red are the names that you will be using in your forms as well

The values in green are to determine what value must be present to be acceptable.

==0 means more characters than zero. <=2 means it must have an input greater than 2 characters.

The values in purple are the comments you wish the script to give when the input is not acceptable.

Make sure to use the email validation as it is listed here; this one really won't stand for anything except text in valid email format.







Privacy Policy