Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    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 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.

  • #2
    New Coder donna1's Avatar
    Join Date
    Nov 2012
    Location
    london
    Posts
    99
    Thanks
    9
    Thanked 4 Times in 4 Posts
    '10' Thats a string right?
    dont you have to convert it to a number before you can use <=

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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.

  • #5
    New Coder
    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.

    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;
    }

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    New Coder
    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;
    }

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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???
    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,636 Times in 4,598 Posts
    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.

  • #10
    New Coder
    Join Date
    Feb 2008
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

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