Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FORM VALIDATION PROBLEM (MM_validateForm)

Status
Not open for further replies.

kristingish

Programmer
Jun 6, 2008
2
I am currently trying to fix a problem on the form submission on my site.

On the Request Information page I am using MM_validateForm function and the FormtoEmail.php script.

When required fields are blank, you are alerted via pop-up notification when hitting submit. Afterwards though, the form still sends without letting you finish completing required fields. I have noticed that if the First Name field is blank the form will not send, but any other field it just still sends.

I have looked this up on Google endlessly and have tried every 'fix'.

This is the function:

Code:
<script type="text/javascript">
<!--
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors=',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' Must Contain an Email Address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' Must Contain a Valid Phone Number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' Must Contain a Number Between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' Is Required.\n'; }
    } if (errors){ alert('Please Complete All Required Fields:\n'+errors); }
	
    document.MM_returnValue = (errors ==');
} }
//-->


This is the form html code:
Code:
 <form action="FormToEmail.php" method="post" name="myform" class="cmxform" id="myform" onsubmit="MM_validateForm('first-name',','R');MM_validateForm('last-name',
','R');MM_validateForm('workphone',','RisNum');MM_validateForm('address1',
','R');MM_validateForm('city',','R');MM_validateForm('state',','R');
MM_validateForm('country',','R');return document.MM_returnValue;">

This is the code on the submit button:
Code:
<input type="submit" class="buttons" onClick="MM_validateForm('first-name',','R');MM_validateForm('last-name',
','R');MM_validateForm('workphone',','RisNum');MM_validateForm('address1',
','R');MM_validateForm('city',','R');MM_validateForm('state',','R');
MM_validateForm('country',','R');return document.MM_returnValue;" value="Send"/>
 
The best way is to do all your validation server-side. That way, no matter whether you have a good or bad client-side validation script, or whether the user has JavaScript disabled or not, you still get validation happening.

Of course, you could beef up your client-side script as well to make for a nicer user experience when JS is enabled, but I'd always opt for server-side validation as well.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top