PDA

View Full Version : Comparing 2 Form Values



bbrant
05-01-2006, 01:01 AM
Hello -

I've been asked to create a new form that will require users to enter their e-mail address and the re-enter it for validation purposes. The form is being designed for use with ColdFusion MX. I know I can do form validation and compare the e-mail address using CF but would rather use JavaScript (similar to this script (http://www.dynamicdrive.com/dynamicindex16/requiredcheck.htm) posted on DynamicDrive) for the validation before my "action" page is hit.

Any thought's or information on comparing the two e-mail address form fields is GREATLY appreciated!!

Thanks,
Brian

djr33
05-01-2006, 01:16 AM
could do it with php easily.

dunno how to get the value of a form field.

but: assuming you can:

in your form tag:
<form action="check()">

in the JS:

function check {
if (value1 == value2) {
document.location.isthistherightsyntax...idon'tknowJSwell,heh; (or... I guess you'd need to submit as well.... i dunno that code either, sorry)
}
else {
alert("oops... your emails don't match"');
}
}


that's the basic idea. Syntax IS off. learning js too.

mwinter
05-01-2006, 11:50 AM
I've been asked to create a new form that will require users to enter their e-mail address and the re-enter it for validation purposes. [...] I know I can do form validation and compare the e-mail address using CF but would rather use JavaScript [...]There's certainly no reason not to use client-side code in addition to server-side validation, but it's never a substitute.



function validate(form) {
var controls = form.elements;

/* Validation.
* ...
*/
if (controls['e-mail'].value != controls['mail-confirmation'].value) {
alert('Your e-mail address and its confirmation do not match.');
return false;
}
/* More validation.
* ...
*/
return true;
}


<form action="..." method="..." onsubmit="return validation(this);">
<table>
<!-- ... -->
<tr>
<th><label for="e-mail">E-mail address</label>:</th>
<td>
<input id="e-mail" type="text" name="e-mail" value="">
</td>
</tr>
<tr>
<th><label for="mail-confirmation">Confirm address</label>:</th>
<td>
<input id="mail-confirmation" type="text" name="mail-confirmation" value="">
</td>
</tr>
<!-- ... -->
</table>
</form>




dunno how to get the value of a form field.The comp.lang.javascript newsgroup FAQ (http://www.jibbering.com/faq/#FAQ4_13) (and the linked notes) will be of use.

Mike