krusty
01-14-2004, 03:56 PM
Hi,
Can someone please tell me what is wrong with the following form validation. It doesn't work 100% accurately. For example, the form will still submit when I type in "a" even though I have stated that values lower than 6 characters not be allowed.
I think I must be using the || operator incorrectly. Any help would be appreciated. Thanks.
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE = "JavaScript">
<!--
function checkForm(form) {
var reg = /\W/
var litmus = true
if (form.firstName.value == "" || reg.test(form.firstName.value) || form.firstName.length < 6 || form.firstName.length > 15){
fName.style.visibility ='visible'
litmus = false
}else{
fName.style.visibility = 'hidden'
}
return litmus
}
//-->
</SCRIPT>
</head>
<body>
<form onSubmit ="return checkForm(this)">
<div id = "fName" style= "visibility:hidden">Please enter your First Name!</div>
<p>First Name:</p>
<input type="text" name="firstName" id="firstName" size="30" maxlength="60" />
<input type="submit" value="Click to Submit" />
</form>
</body>
</html>
Can someone please tell me what is wrong with the following form validation. It doesn't work 100% accurately. For example, the form will still submit when I type in "a" even though I have stated that values lower than 6 characters not be allowed.
I think I must be using the || operator incorrectly. Any help would be appreciated. Thanks.
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE = "JavaScript">
<!--
function checkForm(form) {
var reg = /\W/
var litmus = true
if (form.firstName.value == "" || reg.test(form.firstName.value) || form.firstName.length < 6 || form.firstName.length > 15){
fName.style.visibility ='visible'
litmus = false
}else{
fName.style.visibility = 'hidden'
}
return litmus
}
//-->
</SCRIPT>
</head>
<body>
<form onSubmit ="return checkForm(this)">
<div id = "fName" style= "visibility:hidden">Please enter your First Name!</div>
<p>First Name:</p>
<input type="text" name="firstName" id="firstName" size="30" maxlength="60" />
<input type="submit" value="Click to Submit" />
</form>
</body>
</html>