Forum Moderators: open

Message Too Old, No Replies

textbox validation

textbox validation

         

white300z

2:51 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



I have the following simple form with one textbox:

<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

ajkimoto

3:33 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



white300z,

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

white300z

3:54 pm on Mar 26, 2004 (gmt 0)

10+ Year Member



Thank you so much ajkimoto,

I am primarily a VBScript developer and am familiar with using Modulous. I should have thought of that.

Thank you very much!

Jim