...

View Full Version : complex validity check script



ShMiL
04-06-2003, 11:38 AM
When someone fills the contact form in my website - he has three options to be contacted by: phone number, cellphone number and email.

The user should have the option to fill in one of the options or more (one or two or three, but not none).

The script now runs validity check on all fields (on email, phonenumber AND cellnumber).
I need to change the script so it'll allow the user to fill only two fields (or one, or three - whatever he chooses) and then will run the validity check only on the ones he filled.

*a filled field is a field with more than 0 chars in it.

Thanks for any help!

ConfusedOfLife
04-06-2003, 02:12 PM
<form onsubmit="for (i=0; i<this.elements('contacts[]').length; i++ ) if ( this.elements('contacts[]')[i].value.length != 0 ) chkValidity(this.elements('contacts[]')[i]); ">
Phone Number: <input type="Text" name="contacts[]"><br />
Cell Phone: <input type="Text" name="contacts[]"><br />
Email: <input type="Text" name="contacts[]"><br />
<input type="Submit">
</form>


Note:I assume that your validity check function is chkValidity().

liorean
04-06-2003, 03:04 PM
this.elements('contacts[]')[i]

should really be

this.elements['contacts[]'][i]

ShMiL
04-06-2003, 04:40 PM
Originally posted by ConfusedOfLife


<form onsubmit="for (i=0; i<this.elements('contacts[]').length; i++ ) if ( this.elements('contacts[]')[i].value.length != 0 ) chkValidity(this.elements('contacts[]')[i]); ">
Phone Number: <input type="Text" name="contacts[]"><br />
Cell Phone: <input type="Text" name="contacts[]"><br />
Email: <input type="Text" name="contacts[]"><br />
<input type="Submit">
</form>


Note:I assume that your validity check function is chkValidity().

I don't understand - how come all three fields has the same name?!

cheesebagpipe
04-06-2003, 11:05 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">

function checkform(oForm) {
var el, none = true, re = /^\s*$/, msg = '';
el = oForm.elements['phonenumber'];
if (!re.test(el.value)) {
none = false;
if (!chkPhone(oForm)) return false;
}
el = oForm.elements['cellnumber'];
if (!re.test(el.value)) {
none = false;
if (!chkCell(oForm)) return false;
}
el = oForm.elements['email'];
if (!re.test(el.value)) {
none = false;
if (!chkEmail(oForm)) return false;
}
if (none) {
msg += 'Please enter one of the following so that we may contact you:\n\n';
msg += '\t___your PHONE NUMBER\n';
msg += '\t___your CELL PHONE NUMBER\n';
msg += '\t___your E-MAIL ADDRESS\n\n';
msg += 'God bless you, my child.';
alert(msg);
oForm['phonenumber'].focus();
oForm['phonenumber'].select();
return false;
}
return true;
}

function chkPhone(oForm) {
var field = oForm.phonenumber;
alert(field.value);
//validation here
return true;
}

function chkCell(oForm) {
var field = oForm.cellnumber;
alert(field.value);
//validation here
return true;
}

function chkEmail(oForm) {
var field = oForm.email;
alert(field.value);
//validation here
return true;
}

</script>
</head>
<body>
<form action="javascript&#58;alert('ok')" onsubmit="return checkform(this)">
<strong>Please contact me by:</strong><br />[fill in at least one]<br /><br />
<input type="text" name="phonenumber">____phone_number<br />
<input type="text" name="cellnumber">____cell_phone_number<br />
<input type="text" name="email">____email_address<br /><br />
<input type="submit" value="DONE">
</form>
</body>
</html>

ConfusedOfLife
04-07-2003, 08:09 PM
Originally posted by ShMiL
I don't understand - how come all three fields has the same name?!

I really don't have anything to do with the names, I use the elements array and I iterate it getting their values. It means even if you change the names to anything else it works! But if you wana know why, it's because when you put a name for a form element, when it's submited, you'll get it back in the server side language with the same name. In case of PHP ( a server side language ) it also gives you the option of defining an array name for the elements that you like, then when you get your inputs in PHP, you will have an array to work with. You don't have to remember any name. It's really no need that you do this if you're working client side.

ShMiL
04-10-2003, 12:07 PM
cheesebagpipe - your solution can be very helpful.
Is there a way to run on every one different validity check?
In your example - it checks if one of the fields is filled - is there a way to check if one of those fields:
email
phone_num
cell_num

passes those check-functions:
chkEmail()
chkPhone()
chkCell()
(chkEmail checks email, chkPhone checks phone_num, chkCell checks cell_num)

thanks alot!

beetle
04-10-2003, 03:37 PM
fValidate (http://www.peterbailey.net/fValidate/) does that :D

<input type="text" name="phonenumber" alt="phone|bok">____phone_number<br />
<input type="text" name="cellnumber" alt="phone|bok">____cell_phone_number<br />
<input type="text" name="email" alt="email|bok">____email_address<br /><br />
<input type="hidden" alt="atleast|1|,|phonenumber,cellnumber,email" />

:thumbsup:

cheesebagpipe
04-11-2003, 12:06 AM
ShMiL

Edited the above; hope it's OK.
Assume you've got those check-functions...:cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum