View Full Version : check for a range of numbers

wkilc

12-05-2012, 12:29 AM

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

function formCheckAdults(formobj)

{

if ( formobj.elements['Student'].value == '1' && formobj.elements['Adults'].value == '' )

{

alert('Error: How many adults will be attending?');

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:

function formCheckAdults(formobj)

{

if ( formobj.elements['Student'].value <= '10' && formobj.elements['Adults'].value == '' )

{

alert('Error: How many adults will be attending?');

return false;

}

return true;

}

... but even that does not work.

Thanks.

donna1

12-05-2012, 12:47 AM

'10' Thats a string right?

dont you have to convert it to a number before you can use <=

Old Pedant

12-05-2012, 12:50 AM

Yes, you *MUST* convert to a number!

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

Old Pedant

12-05-2012, 12:54 AM

One way:

function formCheckAdults(formobj)

{

var student = Number( formobj.Student.value ); // why do you use elements['Student']???

var adults = Number( formobj.Adults.value );

if ( student <= 10 && adults <= 0 )

{

alert('Error: How many adults will be attending?');

return false;

}

return true;

}

I got the value of adults that way in case some joker puts in -17 as the number!

wkilc

12-05-2012, 01:31 AM

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.

function formCheckAdults(formobj)

{

for (var i = 1; i <= 20; i++ )

{

if ( formobj.elements['Students'].value == i && formobj.elements['Adults'].value == '' )

{

alert('Error: How many adults will be attending?');

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?

function formCheckAdults(formobj)

{

var student = Number( formobj.Student.value ); // why do you use elements['Student']???

var adults = Number( formobj.Adults.value );

if ( student <= 20 && student >=10 && adults <= 0 )

{

alert('Error: How many adults will be attending?');

return false;

}

return true;

}

felgall

12-05-2012, 01:43 AM

but how do I set it to check between, let's say, 11 and 20?

The easiest way to test that would be:

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:

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).

wkilc

12-05-2012, 02:02 AM

Thanks so much... is this correct?

function formCheckAdults(formobj)

{

var student = +formobj.Student.value;

var adults = +formobj.Adults.value;

if (student > 10 && student < 21) && adults <= 0 )

{

alert('Error: How many adults will be attending?');

return false;

}

return true;

}

Old Pedant

12-05-2012, 02:23 AM

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???

Old Pedant

12-05-2012, 02:24 AM

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...

wkilc

12-05-2012, 03:15 AM

This worked:

function formCheckAdults(formobj)

{

var student = Number( formobj.Students.value );

var adult = Number( formobj.Adults.value );

if ( student > 0 && student < 11 && adult <= 0 )

{

alert('Error: How many adults will be attending?');

return false;

}

return true;

}

Thanks so much! ...I can't find a button to mark the thread RESOLVED.

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.