Validate textbox input using javascript?

They have: 105 posts

Joined: Mar 2006

Hi,

Is it possible to use a regular expression to check a textbox to ensure numbers are between 1 and 5 entered by the user. Confused

I tried the following which is called when the user clicks the submit button:

if ( valueRange(q.value.value < 1 or > 5) )
{
qc.value += "Please enter a number between 1 and 5\n";

}

He has: 698 posts

Joined: Jul 2005

Is it just one number they are entering? (e.g. "3," "4.25839," or "8")
Or do you want to check each number in a string of numbers? (e.g. "34352," "1232.513," "32299084")
Or do you want to check the numbers amongst a string of text? (e.g. "I have 3 apples," "I have 4.25 dollars," "I have 4259 paper clips.")

If it's the first one, I would do something very simple:

if (varName>=1 && varName<=5) {
  ...execute action
}
else {
  ...execute other action
}
'

For the others, the situation would require use of the split() function and I would rather wait to see your answer to code it out.

Kurtis

They have: 105 posts

Joined: Mar 2006

Actually, I just need help with the correct syntax for the following:

if ( valueRange(q.value.value < 1 or > 5) )
{
qc.value += "Please enter a number between 1 and 5\n";

}

How do you wright if value is between 1 and 5? would the above code work if I had the correct operator?

calculator's picture

They have: 40 posts

Joined: Nov 2007

Your complicating things way to much Wink

if(q.value < 1 || q.value > 5){
qc.value += "Please enter a number between 1 and 5\n";
}

the || is equivalent to OR, you can also use && instead of AND

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.