Click to See Complete Forum and Search --> : Validate a word


rjra2
08-04-2003, 02:59 PM
I need help validating a word to continue a form.
I want a user to type AGREE into an text box before they click on the submit button to continue.

Any help would be great!

Thanks

David Harrison
08-04-2003, 03:13 PM
There are a few ways, you could do this:

if(word_entered.toLowerCase()=="agree"){return true;}
else{return false;}

or this:

if(word_entered.toLowerCase().indexOf("agree")>=0){return true;}
else{return false;}

or you could use regular expressions but the browser would need to have js 1.2 for that, and I can't quite remember how to use them right now.

rjra2
08-04-2003, 03:21 PM
<SCRIPT LANGUAGE='JavaScript'>
<!--
function checkFields() {
missinginfo = "";

doorOption = -1
for (i=0; i<orderpackage.package_code_z_s.length; i++) {
if (orderpackage.package_code_z_s[i].checked) {
doorOption = i
}
}
if (doorOption == -1) {
missinginfo += "\n - Vehicle Package";
}

if (document.orderpackage.Agree_Z.value == "") {
missinginfo += "\n - You need to type AGREE to continue";
}

if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"You failed to correctly fill in your:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}

// -->
</SCRIPT>



<form name="orderpackage" action="#" method="post" enctype="encodingtype" onSubmit="return checkFields();">
<input type="radio" name="code_z_s" value="1">
<input type="radio" name="code_z_s" value="2">

<input type="text" name="Agree_Z" value="" size="20" maxlength="5">

<input type="submit" name="goforward3" value="Complete Order">

</form>

elwell
08-04-2003, 03:24 PM
I wrote one with regular expressions. It will probably be the shortest code you code write for this. You can modify it for your needs.

<html><head><title>test</title>
<script language="javascript">
<!-- Hide

re = /^AGREE$/

function submitIt(myForm) {
if (re.test(myForm.validagree.value)) {
return true
}
alert("You Must Enter In All Caps The Word: AGREE")
myForm.validagree.focus()
myForm.validagree.select()
return false
}
// Done Hide -->
</script>
</head>
<body>
<form onSubmit="return submitIt(this)">
<input name="validagree" type="text" size="5">
<input type="submit" value="Submit">
</form>
</body>
</html>