View Full Version : Boolean || operator question.

01-14-2004, 03:56 PM

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.

<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
fName.style.visibility = 'hidden'
return litmus



<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" />



01-14-2004, 04:13 PM
Ok, let's do a breakdown:
Originally posted by krusty
form.firstName.value == "" || reg.test(form.firstName.value) || form.firstName.length < 6 || form.firstName.length > 15
Returns true if:
The value of the form field is is an empty string, OR
The value of the form field matches the regex, OR
The length of the form field is smaller than than 6, OR
The length of the form field is larger than 15
Which seems to be what you want. "Why doesn't it work?" you might ask.

Why don't you alert the length of the form field to have a look?

Now you know it's undefined. What might the reason be?

Yeah, you got it. It's the length of the value of the form field you want, not the length of the actual form field.

01-14-2004, 04:44 PM
Thanks for the help, as you can tell I'm still new to this and I was banging my head against the wall.


01-14-2004, 05:11 PM
a point if you make an if statement that checks if something is higher than a or smaller then b and b is larger than a:


if(x<a || x>b){}

such an if sentence will always be true therefore irrellevant or wrong:

examples of true:


1. x=4

x<a =true
x>b =true
if( true || true){}

2. x=2
x<a =false
x>b =true
if( false|| true){}

3. x=7
x<a = true
x>b = false
if( true|| false){}

as it should be clear no matter what x is the if will always be launched, my guess if that u wanna do an and check(&&) to make sure x is between the 2 numbers.

01-14-2004, 05:19 PM

thanks for the responses, the suggestion from liorean solved the problem.