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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation not doing second part

Status
Not open for further replies.

ironyx

Technical User
Nov 13, 2001
134
US
I have this form validation and I can't see why it won't check for the numeric property. Could someone take a look at it and let me know if I am missing something obvious?
<script language="JavaScript">
function verify(f){
var msg;
var empty_fields = "";
var errors = "";

for (var i = 0; i < f.length; i++) {
var e = f.elements;
if((e.type == "text") && e.required){
if((e.value == null) || (e.value == "")){
empty_fields += "\n " + e.name;
continue;
}
if((e.type == "text") && e.numeric){
var v = parseFloat(e.value);
if(isNaN(v)){
errors += "- The field " + e.name + " must be a number";
}
}
}
}
if (!empty_fields && !errors) return true;

msg = "_______________________________________________________________\n\n"
msg += " The form was not submitted because of the following error(s).\n";
msg += " Please correct these error(s) and re-submit.\n";
msg += "_______________________________________________________________\n\n"

if(empty_fields){
msg += "- The following required field(s) are empty:"
+ empty_fields + "\n";
if(errors) msg += "\n";
}
msg += errors;
alert(msg);
return false;

}
</script>

With the form prpoerties set like:
<form action="#SCRIPT_NAME#" method="post"
onSubmit="
this.AS_OF_DT.required = true;
this.ACTUAL_AM.numeric = true;
this.PLAN_AM.numeric = true;
this.CASH_AM.numeric = true;
return verify(this);
">
Thanks for any insight!
Va :)
 
because you have it stuck in the "if((e.type == "text") && e.required){" block

try this:

Code:
<script language="JavaScript">
function verify(f){
	var msg;
	var empty_fields = "";
	var errors = "";

	for (var i = 0; i < f.length; i++) {
		var e = f.elements[i];

		if((e.type == "text") && e.required){
			if((e.value == null) || (e.value == "")){
				empty_fields += "\n     " + e.name;
				//continue;
			}
		}
		
		if((e.type == "text") && e.numeric){
			var v = parseFloat(e.value);
			if(isNaN(v)){
				errors += "- The field " + e.name + " must be a number\n";
			}
		}
	}
	if (!empty_fields && !errors) return true;

	msg = "_______________________________________________________________\n\n"
	msg += " The form was not submitted because of the following error(s).\n";
	msg += " Please correct these error(s) and re-submit.\n";
	msg += "_______________________________________________________________\n\n"

	if(empty_fields){
		msg += "- The following required field(s) are empty:"
		+ empty_fields + "\n";
		if(errors) msg += "\n" + errors;
	}
	alert(msg);
	return false;
}
</script>


<shameless_plug>
...or,
</shameless_plug>

try my handy version of auto-validation:


=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top