Click to See Complete Forum and Search --> : No remainders on Division?
kelemvor
03-11-2003, 09:17 PM
Hi there,
I haven't used JavaScript and can't remember the syntax.
I want to make sure that a field in a form is always entered in multiples of 100. So I can to do a simple If Value/100 = Value/100 without the remainder, then it's OK. If not I want to pop up a window saying it has to be divisble by 100 and to try again.
But how do I do the part without the remainder?
Thanks.
Try this code (note the Math.floor):
<html>
<head>
<title>Divisible</title>
<script language="javascript" type="text/javascript">
function divisible()
{
val = document.myform.mytext.value;
one = val/100;
two = Math.floor(val/100);
if (one == two)
{
alert ("divisible by 100");
}
else
{
alert ("not divisible by 100");
}
}
</script>
</head>
<body>
<form name="myform">
<input type="text" name="mytext">
<input type="button" onClick="divisible();" value="Check"></form>
</body>
</html>
Dan Drillich
03-11-2003, 10:27 PM
Please try -
<SCRIPT LANGUAGE="JavaScript">
var i = 300;
var j = 100;
var m = i % j;
alert(m);
</SCRIPT>
kelemvor
03-11-2003, 11:00 PM
OK, I got it to work but now it's giving me a weird "Object Expected" error message on this line. (It's FrontPage generated so I don't know what the first part is. Validation stuff mostly.
<!--webbot bot="Validation" s-display-name="Quantity" s-data-type="Integer" b-value-required="False" i-maximum-length="10" --><input type="TEXT" name="Quantity" size="10" value="0" maxlength="10" onChange="Check100()"><br>
This is the code I have
function Check100()
{
var val=document.FrontPage_Form1.Quantity.value;
var one = val/100;
var two = Math.floor(val/100);
if (one <> two)
{
alert ("Tickets must be ordered in quantities of 100");
document.FrontPage_Form1.Quantity.focus();
}
}
It doesn't even give me the Alert window so something's dying prior to that.
AdamBrill
03-11-2003, 11:07 PM
First of all, that first part can get deleted. It should be like this: <input type="TEXT" name="Quantity" size="10" value="0" maxlength="10" onChange="Check100()"><br>As for the object expected, try posting the rest of your code. This part is fine, so there must be a problem in some other part...
In your code, you have if (one <> two), it should be in this format if (one == two) (equals) or if (one != two) (doesn't equal)
kelemvor
03-12-2003, 09:13 AM
Well I changed the != thing and it seems to work now. Not sure if that caused the Object Expected error or if that fixed itself overnight by my shutting down my PCs.
Whatever ther eason, it seems OK now.
The other thing that's not working is after they get the Pop Up saying you need to enter things in quantites of 100, I want that same box to still be highlighted. I tries to use the doc.form.fieldname.focus() but that didn't seem to work.
THanks.
Ok, first of all, it had nothing to do with shutting you computer down. It was your if statement [ if (one <> two) ] that was causing the problem.
View this revision to hightlight text after the alert:
<html>
<head>
<title>Divisible</title>
<script language="javascript" type="text/javascript">
function divisible()
{
val = document.myform.mytext.value;
one = val/100;
two = Math.floor(val/100);
if (one == two)
{
alert ("divisible by 100");
document.myform.mytext.select();
}
else
{
alert ("not divisible by 100");
document.myform.mytext.select();
}
}
</script>
</head>
<body>
<form name="myform">
<input type="text" name="mytext">
<input type="button" onClick="divisible();" value="Check"></form>
</body>
</html>
kelemvor
03-12-2003, 09:54 AM
That didn't seem to work.
Here's my code currently.
function Check100()
{
var val=document.FrontPage_Form1.Quantity.value;
var one = val/100;
var two = Math.floor(val/100);
if (one != two)
{
alert ("Tickets must be ordered in quantities of 100.");
document.FrontPage_Form1.Quantity.select();
}
}
That code gets called by this Form Field
<input type="TEXT" name="Quantity" size="10" value="0" maxlength="10" onChange="Check100()">
If I enter 50 into the Quantity box and press <tab>, it pops up the window like it should, and then after I click OK, it goes down to the next field.
I tested your code, and it works to set hightlight the text in IE6, NN7, Mozilla 1.2 and Opera 7. It doesn't work in NN4.x
kelemvor
03-12-2003, 10:34 AM
Well that's strange. I'm using IE6 and it's just not working for me.
I changed it so that it first does a focus() and then does a select() and that seemed to work for some reason.
HOWEVER, I also had to change the Events from onCHange to onBlur. If I left it as onChange="Check100()" it still didn't work right after the popup. When I changed it to onBlue="Check100()", then it started working. No idea why...
I'm confused on this one. :)
I am testing this on PWS on a 98 machine if that matters because this is also a page that uses ASP.