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