|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hi All,
I've created a asp form which requires the user to enter a number. I'm wondering if there is a way to check to ensure that the number entered is of a certain interval (say 5s) and return an error message if it doesn't meet that criteria. I would prefer to do this with javascript to avoid a trip to the server. As javascript isn't my strong point, any assistance would be greatly appreciated! |
|
#2
|
|||
|
|||
|
this function should do the trick
just change the maxNumber and minNumber to the interval you would like Code:
function checkform(oForm)
{
var maxNumber = 5;
var minNumber = 1;
formNumber = oForm.testnumber.value;
if (isNaN(formNumber))
{
alert("formnumber is not numeric");
return false;
}
if ( formNumber - maxNumber > 0)
{
alert("wrong number");
return false;
}
if ( formNumber - minNumber < 0)
{
alert("wrong number");
return false;
}
}
Call the script by <. form action="your action page" method="post" name="theForm" onSubmit="javascript: return checkForm(this)">
__________________
- Rogier Doekes Last edited by rdoekes : October 14th, 2002 at 10:54 AM. |
|
#3
|
|||
|
|||
|
Thanks a lot for the quick and detailed response! I'm a little unclear as to whether it will actually do what I want, though.
Just to give you a little more of an explanation, I need the users to enter bids in $5 denominations (it's for a online charity auction). If someone types in $31 they should get an error, but if they type in $30, then the form should submit. With your function, if the max number is 5, then wouldn't either 30 or 31 return false because they would be greater than zero? Last edited by aspnewbie : October 14th, 2002 at 12:23 PM. |
|
#4
|
|||
|
|||
|
modulus 5
Ok, I think the trick is to use the integer division function modulus in javascript (%)
Code:
function checkform(oForm)
{
var intervalNumber = 5;
formNumber = oForm.testnumber.value;
if (isNaN(formNumber))
{
alert("formnumber is not numeric");
return false;
}
if ( formNumber % intervalNumber > 0)
{
alert("wrong number");
return false;
}
}
hope this helps |
|
#5
|
|||
|
|||
|
Awesome. This looks great. It shows that I should really learn more javascript, given all its functionality!
Thanks again for your help! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > ASP Development > Requiring User to Enter Number Which is a Certain Interval |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|