Jump to content

Merging Two Field Validation Scripts


wychegnome
 Share

Recommended Posts

I am struggling with trying to get two sets of validation to work together on a form. The main set of validation is set up using Dreamweaver 8 and that provides these two scripts which sit in the header section of the page: <script type="text/JavaScript">function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);} if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document); if(!x && d.getElementById) x=d.getElementById(n); return x;} function MM_validateForm() { //v4.0 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=MM_findObj(args); 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 e-mail address.\n'; } else if (test!='R') { num = parseFloat(val); if (isNaN(val)) errors+='- '+nm+' must contain a 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') { if (nm == "recaptcha_response_field") { errors += '- Anti Spam Test must be completed.\n'; } else errors += '- '+nm+' required please.\n'; } } } if (errors) alert('Please correct the following error(s):\n'+errors); document.MM_returnValue = (errors == '');}</script>In the second of the scripts I have successfully added additional validation to pick up that the reCaptcha field hasn't been completed. The validation is invoked using the form's onsubmit function: <form method="post" action="file.asp" onsubmit="MM_validateForm('firstName','','R','lastName','','R','address1','','R','city','','R','zip','','R','email','','RisEmail','email2','','RisEmail');return document.MM_returnValue"> (the arguments are in sets of three which it why it looks odd, the sets of three are 1. input field name, 2. unknown but not used, and 3 the settings to check for). I have a separate script which also resides in the header section: <script type="text/javascript">function checkEmail(theForm) { if (theForm.email.value != theForm.email2.value) { alert('eMail addresses don\'t match!'); return false; } else { return true; }}</script> This is designed to compare the content of two input fields and return an alert message if they don't match. If I invoke the email checker using the form's onsubmit function in place of the Dreamweaver scripts it works as intended. However, if I use it in the email2 input field either in the form onblur="checkEmail(this);" or onchange="checkEmail(this);" or onsubmit="checkEmail(this);" in conjuction with the Dreamweaver supplied code located in the form's onsubmit instruction it fails to activate and no error is generated when email and email2 fields hold differing contents. In essence this works:<form method="post" action="file.asp" onsubmit="MM_validateForm('firstName','','R','lastName','','R','address1','','R','city','','R','zip','','R','email','','RisEmail','email2','','RisEmail');return document.MM_returnValue"> so does this:<form method="post" action="file.asp" onsubmit="checkEmail(this);"> but this combination doesn't:<form method="post" action="file.asp" onsubmit="MM_validateForm('firstName','','R','lastName','','R','address1','','R','city','','R','zip','','R','email','','RisEmail','email2','','RisEmail');return document.MM_returnValue"><input name="email" type="text" /><input name="email2" type="text" onblur="checkEmail(this);" /> What I need to do is check the form on a single press of the submit button to validate that the 'required' fields have entries and they of the correct type (text, number, email etc) and that the two email fields contain identical entries so as to minimise the risk of a mis-typed email address. Any help of guidance on how to merge the two varities of checking would be appreciated. Thanks, John

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...