Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: check for a range of numbers

1. ## check for a range of numbers

I have a form validation script than can check for a specific value, i this case 1 student:

Code:
```function formCheckAdults(formobj)

{
if ( formobj.elements['Student'].value == '1' && formobj.elements['Adults'].value == '' )
{
return false;
}
return true;
}```
I want to change it so it will check a range of Student values... if the value was 1-10, or 11-20, or 21-30, etc...

I had figured this to get started:

Code:
```function formCheckAdults(formobj)

{
if ( formobj.elements['Student'].value <= '10' && formobj.elements['Adults'].value == '' )
{
return false;
}
return true;
}```
... but even that does not work.

Thanks.

2. '10' Thats a string right?
dont you have to convert it to a number before you can use <=

3. Yes, you *MUST* convert to a number!

Because '10' is less than '2'! (And '3' and '4' and ... '9'.)

4. One way:
Code:
```function formCheckAdults(formobj)
{
var student = Number( formobj.Student.value ); // why do you use elements['Student']???
if ( student <= 10 && adults <= 0 )
{
return false;
}
return true;
}```
I got the value of adults that way in case some joker puts in -17 as the number!

5. Thank you. If I follow, the '10' was not a string, but an actual value from the form field.

I tried this, but didn't know how to use the 'i' in the if statement.

Code:
```function formCheckAdults(formobj)
{
for (var i = 1; i <= 20; i++ )
{
if ( formobj.elements['Students'].value == i && formobj.elements['Adults'].value == '' )
{
return false;
}
}
return true;
}```
This is great, thanks... but how do I set it to check between, let's say, 11 and 20? Is this it?

Code:
```function formCheckAdults(formobj)
{
var student = Number( formobj.Student.value ); // why do you use elements['Student']???
if ( student <= 20 && student >=10 && adults <= 0 )
{
return false;
}
return true;
}```

6. Originally Posted by wkilc
but how do I set it to check between, let's say, 11 and 20?
The easiest way to test that would be:

Code:
`if (student > 10 && student < 21)`
Note also that ALL values entered into form fields are text strings and not numbers so you need to convert it to a number first - for example:

Code:
`students = +formobj.elements['Student'].value;`
the + being the shortest way to tell it to convert a string to a number (wrapping the variable after the + inside Number() would do the same thing).

7. Thanks so much... is this correct?

Code:
```function formCheckAdults(formobj)
{
var student = +formobj.Student.value;
if (student > 10 && student < 21) && adults <= 0 )
{
return false;
}
return true;
}```

8. Form field values are *ALWAYS* strings. Always.

If you want them to be *TREATED* as numbers, you must convert them.

There are many ways to do that. The way I used Number( ...string value... ) is just one of them. But it's probably the easiest to understand.

********

but didn't know how to use the 'i' in the if statement.
I have no idea what i is supposed to be.

What is it you have 20 of???

9. And, yes, prepending a unary plus is another way to convert a string to a number.

So is parseFloat(string) and parseInt(string, radix)

And and and...

10. This worked:

Code:
```function formCheckAdults(formobj)
{
var student = Number( formobj.Students.value );
if ( student > 0 && student < 11 && adult <= 0 )
{
return false;
}
return true;
}```
Thanks so much! ...I can't find a button to mark the thread RESOLVED.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•