PDA

View Full Version : Help with Dreamweaver Validate form javascript



monaya
08-23-2013, 01:55 AM
I have this javascript Dreamweaver creates by default to validate a form from errors. I would like to run another script IF there are no errors and the form is submitted, but I just can't figure out where the form is submitted in this javascript. Basically I want to stop duplicate submissions.


Here's what I want to run when the form is submitted:


document.getElementById('loadingmsg').innerHTML = "<img src='/images/ajax-loadergray.gif' /> Working... ";


Here's the Dreamweaver default validation form (I'm ok with JS but I can't read this well):


<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 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') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');

}

}
//-->
</script>

jscheuer1
08-23-2013, 03:35 AM
It's hard to say with certainty without seeing how this code is assigned to the form. But it looks like the form's submission is contingent upon this value being true:


document.MM_returnValue = (errors == '');

If there are no errors, this will be true, and presumably the form submits. As I say I cannot be certain though without seeing the form, preferably the entire page. But if I'm right, and successful submission does not cause the page to reload or to load another page, this should work (addition highlighted):


<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 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') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
if(document.MM_returnValue){
document.getElementById('loadingmsg').innerHTML = "<img src='/images/ajax-loadergray.gif' /> Working... ";
}
}

}
//-->
</script>



The browser cache may need to be cleared and/or the page refreshed to see changes.

If you want more help, please include a link to the page on your site that contains the problematic code so we can check it out.