...

View Full Version : All round form validator



mmasci
07-23-2002, 07:15 AM
I was wanting to develop a script that will validate text fields and radio buttons in the same function, without the overlap in code.

Please help:confused: :confused: :confused:

mmasci

mordred
07-23-2002, 09:39 AM
Don't be reluctant - feel free to post how far you've come.

If it helps, you can pass a reference to the entire form as a parameter to the validating function. Say for instance that your function is called validator, then you'd have



function validator(formRef) {
// formRef does now point to the form we're validating
if (formRef.elements["shippingTime"].value) {
// etc. pp.
}
}


and call it from somewhere in the HTML like this

<form name="myForm" onsubmit="return validator(this)">

The "this" keyword points now at your form myForm, and you can work upon its various input elements. Hope that helps and did not confuse you anymore. :)

adios
07-23-2002, 08:06 PM
A stripped-down version, but might help. You can add more checks, as desired.



<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

var msg = new Object(); //prompts, indexed by field name
msg['sample1'] = 'Sample 1?';
msg['sample2'] = 'Sample 2?';
msg['more'] = 'Punch a "more" radio, dude!';
msg['extra'] = 'Type in something extra, please.';

function checkform(f) {
var el, e = 0;
while (el = f.elements[e++]) {
switch (el.type) {
case 'text' :
case 'textarea' :
if (!el.value) {
alert(msg[el.name]);
el.focus();
return false;
}
break;
case 'radio' :
var rad, r = -1, grp = f[el.name];
while (rad = grp[++r]) if (rad.checked) break;
if (r == grp.length) {
alert(msg[el.name]);
el.focus();
return false;
}
else e += grp.length-1;
break;
}
}
return true;
}

</script>
</head>
<body>
<form action="javascript&#58;alert('Submitted !')" method="post"
onsubmit="return checkform(this)">
<input name="sample1" type="text"> sample 1<br>
<input name="sample2" type="text"> sample 2<br><br>
<input name="more" type="radio"> more<br>
<input name="more" type="radio"> lots more<br><br>
<input name="extra" type="text"> extra<br><br>
<input type="submit" value="DONE">
</form>
</body>
</html>

mmasci
07-31-2002, 03:52 AM
This script is great!!

But what if I have some fields in the form that are not required fields and may not need a response from the user??

How can this script be modified to accomodate this possibility??

Cheers,

mmasci:( :eek: :eek:

glenngv
07-31-2002, 06:26 AM
you can append "_required" in the element's name that is required.
i modified adios' code to illustrate what i mean...



<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

var msg = new Object(); //prompts, indexed by field name
msg['sample1_required'] = 'Sample 1?';
msg['sample2_required'] = 'Sample 2?';
msg['more_required'] = 'Punch a "more" radio, dude!';
msg['extra'] = 'Type in something extra, please.';

function checkform(f) {
var el, e = 0;
while (el = f.elements[e++]) {
if (el.name.indexOf("_required")==-1) continue;
switch (el.type) {
case 'text' :
case 'textarea' :
if (!el.value) {
alert(msg[el.name]);
el.focus();
return false;
}
break;
case 'radio' :
var rad, r = -1, grp = f[el.name];
while (rad = grp[++r]) if (rad.checked) break;
if (r == grp.length) {
alert(msg[el.name]);
el.focus();
return false;
}
else e += grp.length-1;
break;
}
}
return true;
}

</script>
</head>
<body>
<form action="javascript&#58;alert('Submitted !')" method="post"
onsubmit="return checkform(this)">
<input name="sample1_required" type="text"> sample 1<br>
<input name="sample2_required" type="text"> sample 2<br><br>
<input name="more_required" type="radio"> more<br>
<input name="more_required" type="radio"> lots more<br><br>
<input name="extra" type="text"> extra<br><br>
<input type="submit" value="DONE">
</form>
</body>
</html>

adios
08-01-2002, 01:18 AM
glenngv fan here...but...

Don't do it this way. It interferes with the naming of the fields - significant for cgi/server processes - and, anyway, it's unnecessary:

http://www.codingforums.com/showthread.php?threadid=2927

No ego here, stole this idea from D. Flanagan :o



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum