Form Validation Script:

<script language="javascript">

function handler(where){
//check for unusual characters
var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
var wspace = " ";
for (var i = 0; i < where.value.length; i++) {
	if (iChars.indexOf(where.value.charAt(i)) != -1) {
	return false;
	}
	if (wspace.indexOf(where.value.charAt(i)) != -1) {
	return "nope";
	}
}
} 

function emailhandler(where){
//check for unusual characters
var iChars = "!#$%^&*()+=-[]\\\';,/{}|\":<>?";
var wspace = " ";
for (var i = 0; i < where.value.length; i++) {
	if (iChars.indexOf(where.value.charAt(i)) != -1) {
	return false;
	}
	if (wspace.indexOf(where.value.charAt(i)) != -1) {
	return "nope";
	}
}
}

function isNumberKey(evt,where)
      {
		var charCode = (evt.which) ? evt.which : event.keyCode;
			if (charCode != 8){
			  var count = where.value.length; 
		      if (count === 0){
				  where.value = "(";
			  }
			  if (count == 4){
				  where.value = where.value + ")";
			  }
			  if (count == 8){
				  where.value = where.value + "-";
			  }
			  if (charCode > 31 && (charCode < 48 || charCode > 57))
				{
				return false;
				}
				else
				{        
				 return true;
				}
			}
			else
			{
			return true;
			}
      }

function checkform()
{
var mycount = document.getElementsByTagName('input').length;
var good = true;
	for (i=0;i<=mycount;i++)
	{
		//alert(document.getElementsByTagName('input')[i].id);
		var current = document.getElementsByTagName('input')[i];
		current.style.border='1px solid';	
		//check email
		if (current.id == "email"){
			if (current.value !== ""){
				var test = emailhandler(current);
				if(test=="nope"){
					alert("Your Email Address can not contain spaces!");
					//good = false;
				}
				if(test===false){
				alert("Your Email Address can not contain special characters!");
				//good = false;
				}
			}
		}
		else
		{
			if(current.id !== "click"){
				if (current.value !== ""){
				var newtest = handler(current);
					if (newtest===false){
						current.style.border='3px solid red';
						alert("You have special characters in the field highlighted \r\n" + current.id);
						good = false;
					}
				}
			}
		}
	}
}
return good;
}

function submitform(){
var check = checkform();
alert(check);
}
</script>

If there is an issue in the form I do get the warnings, but The Alert in submitform() is never triggered.
I'm not sure where I'm buggered. Any help is appreciated. If you can streamline the JS that wouldn't be rejected either.

Use a single function that will validate the entire fields...

Hello,

First let me start with the fact that JavaScript is a human language designed to give instructions to an application. As such, it should be used in a manner that is easy for people to read. Proper formatting is critical to allowing people to understand the instructions so that errors are not made that would cause the application to fail the execution of the script. That said, I found that in function checkform() you placed too many end brackets and is the most logical cause of the script failing.

I have taken the time to cleanup your code. However, without the html part of your page it is impossible to do any further troubleshooting. I would strongly suggest using an editor that is designed to help people work with code like Notepad++. Here is the cleaned up code:

function handler(where){ //check for unusual characters
  var iChars="!@#$%^&*()+=-[]\\\';,./{}|\":<>?",wspace = " ";
  for(var i=0;i<where.value.length;i++){
    if(iChars.indexOf(where.value.charAt(i))!=-1){return false}
    if(wspace.indexOf(where.value.charAt(i))!=-1){return "nope"}
  }
} 


function emailhandler(where){ //check for unusual characters
  var iChars = "!#$%^&*()+=-[]\\\';,/{}|\":<>?",wspace = " ";
  for(var i = 0; i < where.value.length; i++){
    if(iChars.indexOf(where.value.charAt(i)) != -1){return false}
    if(wspace.indexOf(where.value.charAt(i)) != -1){return "nope"}
  }
}


function isNumberKey(evt,where){
  var charCode=(evt.which)?evt.which:event.keyCode;
  if(charCode!=8){
    var count=where.value.length; 
    if(count===0){where.value="("}
    if(count==4){where.value+=")"}
    if(count==8){where.value+="-"}
    if(charCode>31&&(charCode<48||charCode>57)){return false}
    else{return true}
  }
  else{return true}
}


function checkform(){
  var mycount=document.getElementsByTagName("input").length;
  var good=true;
  for(i=0;i<=mycount;i++){

    //alert(document.getElementsByTagName("input")[i].id);

    var current=document.getElementsByTagName('input')[i];
    current.style.border="1px solid"; 

    //check email
    if(current.id=="email"){
      if(current.value!==""){
        var test=emailhandler(current);
        if(test=="nope"){
          alert("Your Email Address can not contain spaces!");
          //good=false;
        }
        if(test===false){
          alert("Your Email Address can not contain special characters!");
          //good=false;
        }
      }
    }
    else{
      if(current.id!=="click"){
        if(current.value!==""){
          var newtest=handler(current);
          if(newtest===false){
            current.style.border="3px solid red";
            alert("You have special characters in the field highlighted \r\n" + current.id);
            good=false;
          }
        }
      }
    }
  }
  return good;
}


function submitform(){
  var check=checkform();
  alert(check);
}

Still doesnt' work :(

<script language="javascript">

function showship(show){

if(show.checked){
document.getElementById("test").style.display = 'none';
}
else
{
document.getElementById("test").style.display = 'block';
}
}

function handler(where){ //check for unusual characters
  var iChars="!@#$%^&*()+=-[]\\\';,./{}|\":<>?",wspace = " ";
  for(var i=0;i<where.value.length;i++){
    if(iChars.indexOf(where.value.charAt(i))!=-1){return false}
    if(wspace.indexOf(where.value.charAt(i))!=-1){return "nope"}
  }
} 

function emailhandler(where){ //check for unusual characters
  var iChars = "!#$%^&*()+=-[]\\\';,/{}|\":<>?",wspace = " ";
  for(var i = 0; i < where.value.length; i++){
    if(iChars.indexOf(where.value.charAt(i)) != -1){return false}
    if(wspace.indexOf(where.value.charAt(i)) != -1){return "nope"}
  }
}

function isNumberKey(evt,where){
  var charCode=(evt.which)?evt.which:event.keyCode;
  if(charCode!=8){
    var count=where.value.length; 
    if(count===0){where.value="("}
    if(count==4){where.value+=")"}
    if(count==8){where.value+="-"}
    if(charCode>31&&(charCode<48||charCode>57)){return false}
    else{return true}
  }
  else{return true}
}


function checkform(){
  var mycount=document.getElementsByTagName("input").length;
  var good=true;
  for(i=0;i<=mycount;i++){

    //alert(document.getElementsByTagName("input")[i].id);

    var current=document.getElementsByTagName('input')[i];
    current.style.border="1px solid"; 

    //check email
    if(current.id=="email"){
      if(current.value!==""){
        var test=emailhandler(current);
        if(test=="nope"){
          alert("Your Email Address can not contain spaces!");
          //good=false;
        }
        if(test===false){
          alert("Your Email Address can not contain special characters!");
          //good=false;
        }
      }
    }
    else{
      if(current.id!=="click"){
        if(current.value!==""){
          var newtest=handler(current);
          if(newtest===false){
            current.style.border="3px solid red";
            alert("You have special characters in the field highlighted \r\n" + current.id);
            good=false;
          }
        }
      }
    }
  }
 alert("should Return");
  return good;
}


function submitform(){
  var check=checkform();
  alert(check);
}

</script>
<style type="text/css">
body {
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:11px;
}
#ship {
	visibility: hidden;
}
#test {
	display: none;
}

</style>
</head>
<body>

<div id="background">
<div align="center">
<div align="left" style="width:40%">
   <div id="uinfo" >
     <form name="uform" method="post" action="cust_info_handler.php">
      <table>
       <tr><td colspan="2">
        <div id="top">
            User Information
        </div>
        </td></tr>
        <tr><td>
                User Name :
			</td>
            <td>
			<?php echo $row['LoginName'];?>
			</td>
	    </tr>


		<tr><td>
                Password : 
            </td>
            <td>
                <input name="password" type="password" id="password" maxlength="15" value="<?php echo $row['Password'];?>" />
			</td>
		</tr>    
    
        <tr><td>
                Email : 
            </td>
            <td>
                <input name="email" type="text" id="email" value="<?php echo $row['Email'];?>">
    		</td>
		</tr>
        </table>
  <div id="cinfo" style="width:40%">
        <table><tr><td colspan="2"><div id="top">
        	Contact Information
		</div></td></tr>
        <tr><td>First Name:</td><td><input name="fname" type="text" id="fname" value="<?php echo $row['Name_First'];?>" size="30"/></td></tr>
        <tr><td>Last Name:</td><td><input name="lname" type="text" id="lname" value="<?php echo $row['Name_Last'];?>" size="50" /></td></tr>
        <tr><td>Day Phone:</td><td><input name="dphone" maxlength="13" onkeypress="return isNumberKey(event, this)" type="text" id="dphone" value="<?php echo $row['Phone_Day'];?>" /></td></tr>
        <tr><td>Message Phone:</td><td><input name="mphone" maxlength="13" onkeypress="return isNumberKey(event, this)" type="text" id="mphone" value="<?php echo $row['Phone_Message'];?>" /></td></tr>
        <!---//Cell Phone NEEDED\\ --->
        <tr><td>Fax Number:</td><td><input name="fax" maxlength="13" onkeypress="return isNumberKey(event, this)" type="text" id="fax" value="<?php echo $row['Fax'];?>" /></td></tr>
        <tr><td>Company Name:</td><td><input name="cname" type="text" id="cname" value="<?php echo $row['CompanyName'];?>" size="120"/></td></tr>
        <tr><td>Website:</td><td><input name="www" type="text" id="www" value="<?php echo $row['WebAddress'];?>" size="120"/></td></tr>
		</table>

  </div>

<div id="minfo">
    <table>
       <tr><td colspan="2"><div id="top">
        	Mailing Address
		</div></td></tr>
        <tr><td>Street Address 1:</td><td><input name="maddress" size="45" type="text" id="maddress" value="<?php echo $row['MailAddress1'];?>" /></td></tr>
		<tr><td>Street Address 2:</td><td><input name="maddress2" size="45" type="text" id="maddress2" value="<?php echo $row['MailAddress2'];?>" />
        <tr><td>City:</td><td><input name="mcity" type="text" id="mcity" value="<?php echo $row['MACity'];?>" /></td></tr>
        <tr><td>State:</td><td><input name="mstate" type="text" size="2" maxlength="2" id="mstate" value="<?php echo $row['MAState'];?>" /></td></tr>
		<tr><td>Zip: </td><td><input type="text" id="mzip" value="<?php echo $row['MAZip'];?>"/></td></tr>
        <tr><td>Country:</td><td><input name="mcountry" type="text" id="mcountry" value="<?php echo $row['MACountry'];?>" /></td></tr>
	</table>
</div>

Ship to same address: <input name="vship" type="checkbox" id="vship" checked="checked" onclick="showship(this)" />

<div id="test">
<table>
<tr><td colspan="2"><div id="top">Shipping Address</div></td></tr>
<tr><td>Street Address 1: </td><td><input type="text" id="saddress" value="<?php echo $row['ShippingAddress1'];?>"/></td></tr>
<tr><td>Street Address 2: </td><td><input type="text" id="saddress2" value="<?php echo $row['ShippingAddress2'];?>"/></td></tr>
<tr><td>City: </td><td><input type="text" id="scity" value="<?php echo $row['SACity'];?>"/></td></tr>
<tr><td>State: </td><td><input type="text" id="sstate" value="<?php echo $row['SAState'];?>"/></td></tr>
<tr><td>Zip: </td><td><input type="text" id="szip" value="<?php echo $row['SAZip'];?>"/></td></tr>
<tr><td>Country: </td><td><input type="text" id="scountry" value="<?php echo $row['SACountry'];?>"/></td></tr>
</table>
</div>

<div><br><input type="button" name="click" id="click" value="submit" onclick="submitform()" ></div></div>
</form>
</div>
</div>

</body>
</html>

I have added the majority of the page to this code, at least enough to make it work for anyone trying to test it out.

I use Dreamweaver CS4 and Notepad++ to write my code.

for (i=0;i<=mycount;i++) needed to be: for (i=0;i<mycount;i++)

How's it going, have you solved it?

Needs to be cleaned up but I am using this:

function validate(where){
var here = where.id;
if(here != "email"){
	if(here != "www"){
		if(here != "dphone"){
			if(here != "cphone"){
				if(here != "mphone"){
					if(here != "fax"){
						if(here != "myclick"){
							if(here != "vship"){
								var newtest = handler(where);
								if(newtest===false){return false;}else{return true;}
							}
						}
					}
				}
			}
		}
	}
}

}
//-->

function testing(){
var mycount = document.uform.elements.length;
var where = document.uform;
var goodtogo = true;
	if (where.email.value!==""){
		var test=emailhandler(where.email);
		if(test===false){goodtogo=false;}
	}
for(i=0;i<mycount;i++){
var test=validate(where.elements[i]);
if(test===false){goodtogo=false;}
}
return(goodtogo);
}

if you could tell me why this doesn't work that would be great:

if((here != "email")||(here != "dphone")||(here != "cphone")||(here != "mphone")||(here != "fax")||(here != "myclick")||(here != "vship")){
			alert(here);
}

Thanks, I've tried to use it in my own app.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.