Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-05-2012, 12:29 AM   PM User | #1
wkilc
New Coder

 
Join Date: Feb 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
wkilc is an unknown quantity at this point
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 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 == '' )
		{
		alert('Error: How many adults will be attending?');
		return false;
		}
	return true;
}
... but even that does not work.

Thanks.
wkilc is offline   Reply With Quote
Old 12-05-2012, 12:47 AM   PM User | #2
donna1
New Coder

 
Join Date: Nov 2012
Location: london
Posts: 55
Thanks: 5
Thanked 1 Time in 1 Post
donna1 can only hope to improve
'10' Thats a string right?
dont you have to convert it to a number before you can use <=
donna1 is offline   Reply With Quote
Old 12-05-2012, 12:50 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-05-2012, 12:54 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
One way:
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;
}
I got the value of adults that way in case some joker puts in -17 as the number!
__________________
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.
Old Pedant is offline   Reply With Quote
Old 12-05-2012, 01:31 AM   PM User | #5
wkilc
New Coder

 
Join Date: Feb 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
wkilc is an unknown quantity at this point
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 == '' )
		{
		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?

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;
}
wkilc is offline   Reply With Quote
Old 12-05-2012, 01:43 AM   PM User | #6
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,455
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by wkilc View Post
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).
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-05-2012, 02:02 AM   PM User | #7
wkilc
New Coder

 
Join Date: Feb 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
wkilc is an unknown quantity at this point
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;
}
wkilc is offline   Reply With Quote
Old 12-05-2012, 02:23 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.

********

Quote:
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???
__________________
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.
Old Pedant is offline   Reply With Quote
Old 12-05-2012, 02:24 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 12-05-2012, 03:15 AM   PM User | #10
wkilc
New Coder

 
Join Date: Feb 2008
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
wkilc is an unknown quantity at this point
This worked:

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;
}
Thanks so much! ...I can't find a button to mark the thread RESOLVED.
wkilc is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:10 AM.


Advertisement
Log in to turn off these ads.