Ezilon.com - Target Your Audience, be Seen in Your Region

Validate textbox input using javascript?

You are viewing this site as a guest. Join our community to get your questions answered and share knowledge. Active members may advertise and ask for a website critique.

He has: 107 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";

}

mscreashuns's picture

He has: 505 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.

He has: 107 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