Forum Moderators: open
<form method="post" action="thispage.htm">
<input type="text" name="Quantity" size="2" tabindex="25">
<input type="submit" value="Continue" tabindex="49">
</form>
How can I add JavaScript to validate that whatever number entered in the "Quantity" textbox is a number that
is divisible by 30? ( for example only 30, 60, 90, 120... and so on are only allowed)
If it isn't divisible by 30, I should get an alert saying that the number entered is not divisble by 30.
Thanks,
Jim
First of all: Welcome to Webmaster World!
As for your question, it is not too difficult.
First, add the following script to your document:
<script type="text/javascript">
<!--
function check30(obj){
//use the modulus operator '%' to see if there is a remainder
if(obj.value%30!=0){
alert('The number you entered is not divisible by 30')
}
}
//-->
</script>
Next, add an onchange event handler to your input object:
<input type="submit" value="Continue" tabindex="49" onchange="check30(this)">
When the user changes the value of the box and hits CR or moves on to another object, the check30() function will fire. Note that we are passing an object reference to the fuction so that it will know which textbox to check.
Hope this helps,
ajkimoto