Enjoy an ad free experience by logging in. Not a member yet? Register.


Results 1 to 10 of 10
Thread: check for a range of numbers

12052012, 12:29 AM #1
 Join Date
 Feb 2008
 Posts
 23
 Thanks
 0
 Thanked 0 Times in 0 Posts
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 == '' ) { alert('Error: How many adults will be attending?'); return false; } return true; }
I had figured this to get started:
Code: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; }
Thanks.
12052012, 12:47 AM
#2
'10' Thats a string right?
dont you have to convert it to a number before you can use <=
12052012, 12:50 AM
#3
Yes, you *MUST* convert to a number!
Because '10' is less than '2'! (And '3' and '4' and ... '9'.)
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
12052012, 12:54 AM
#4
One way:
I got the value of adults that way in case some joker puts in 17 as the number!Code: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; }
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
12052012, 01:31 AM
#5
 Join Date
 Feb 2008
 Posts
 23
 Thanks
 0
 Thanked 0 Times in 0 Posts
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.
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) { 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; }
Code: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; }
12052012, 01:43 AM
#6
 Join Date
 Sep 2005
 Location
 Sydney, Australia
 Posts
 6,458
 Thanks
 0
 Thanked 632 Times in 622 Posts
The easiest way to test that would be:
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:if (student > 10 && student < 21)
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).Code:students = +formobj.elements['Student'].value;
Stephen
Learn Modern JavaScript  http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
Don't forget to start your JavaScript code with"use strict";
which makes it easier to find errors in your code.
12052012, 02:02 AM
#7
 Join Date
 Feb 2008
 Posts
 23
 Thanks
 0
 Thanked 0 Times in 0 Posts
Thanks so much... is this correct?
Code: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; }
12052012, 02:23 AM
#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.
********
I have no idea what i is supposed to be.but didn't know how to use the 'i' in the if statement.
What is it you have 20 of???
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
12052012, 02:24 AM
#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...
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
12052012, 03:15 AM
#10
 Join Date
 Feb 2008
 Posts
 23
 Thanks
 0
 Thanked 0 Times in 0 Posts
This worked:
Thanks so much! ...I can't find a button to mark the thread RESOLVED.Code: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; }