When I validate several fields "on submit" the form is submitted if the final field checked is valid even if other fields checked earlier in the order are not valid. The non-valid fields are reported properly but the form is submitted anyway as long as the final field is valid.
I think I understand why this happens, given the logic of the scripts involved, but I still don't know how to ensure that all fields are valid before submission. As I understand it, validating field by field, for example "on blur", only works if the user actually enters and then exits a field. If they don't enter the field the validation is not performed.
What is the most efficient way to ensure that all fields, not just the final field checked, are valid prior to submission? One solution is forcing a set progression from field to field. Can this be done and over-ride a simple mouse click to a field not in sequence?
Obviously, I am fairly new to html. However, I've spent some time on this without resolution and could really use some help.
for (a = 1; a < 3; a++) { // 3: number of fields;
if (document.formName.fieldName[a].length < 6) {
alert("The field must contain at least 6 characters");
}
if (document.formName.fieldName[a].value == "") {
alert("You must enter a value.");
}
}
</script>
This might have the same problem as your script, but I don't know.
This script has not been tested.
Last edited by Zach Elfers; 01-19-2003 at 11:10 AM.
Sorry for the long post below. I am including more, rather than less because of my inexperience. I think the first script is not related to my current problem. The relevant script begins at "function MM_validateForm() { //v4.0". As I understand it the form action then calls this script for each of the fields designated and with the parameters given.
My problem is that entering a number between 60 and 400 for "Fee Total" makes this field valid and the script is submitted even though "First Name Guardian 1" is blank.
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[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>
...
<FORM ACTION="http://www.formhandler.com/service?144013" METHOD=POST onSubmit="MM_validateForm('First Name Guardian 1','','R');MM_validateForm('Fee total','','RinRange60:400');return document.MM_returnValue">
The reason that your script is not working is because the onSubmit handler only validates what the parameters given are. To keep everthing validated, do something like this:
<input onBlur="MM_validateForm('First Name Guardian 1','','R');">
Do that for every field and enter new parameters and it should work.
Or you might be able to just reverse it like this:
<FORM ACTION="http://www.formhandler.com/service?144013" METHOD=POST onSubmit="MM_validateForm('Fee total','','RinRange60:400');MM_validateForm('First Name Guardian 1','','R');return document.MM_returnValue">
I tried reversing the validation order as you suggest but the form is still submitted when the second but not the first field is valid.
I think that
<input onBlur="MM_validateForm('First Name Guardian 1','','R');">
will also only work if a user enters each of the designated fields.
How does one keep track of whether any field has failed vaildation at the time of submissions and return then the user to the form page, without submission, to correct those fields?
Hi,
i would do this using some javascript and some ASP.
<script language="JavaScript">
<!--
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[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') {
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 (val<min || max<val) 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>
this will only validate textboxes and text areas and will not validate radio buttons and checkboxes.
so now if you post the form back to the same page you can use some ASP to help vaidate the rest then ddirect the user to the required page.
for example:
start your page like this
<%
if submit = "yes" then
'----------start to validate the form--------------
else
'---------display the form-----------------------
end if
%>
ok so what im saying is that if the submit button has been click then the form is posted back to its self it runs this code gets a yes from the submit button so it runs the rest of your validate, but if the sumbit value is "nothing" then that means its the first time in and the form has not be submitted yet therefore display the form.
ok so now you need to put some validation code in
<%
if submit = "yes" then
'----------start to validate the form--------------
if textbox1 = "" then
textbox1error = "empty"
else
textbox1error = ""
end if
if radiobutton1 = "" then
radiobutton1error = "empty"
else
radiobutton1 = ""
end if
'---------ok so you need to send to the correct place------
if textbox1error = "" and radiobutton1error = "" then
response.redirect "/newpage.asp"
else
'-----------------Display your form again and show any errors
end if
Bookmarks