...

View Full Version : javascript input validation



Izzy123
04-15-2010, 12:54 PM
Anyone know how do I use javascript to limit the numeric amount enetered in a text box less than 1000?

Dormilich
04-15-2010, 12:55 PM
x < 1000 ?

Izzy123
04-15-2010, 01:23 PM
here's my code so far, we are meant to make the amount entered in the text box be numeric, not negative and no greater than 1000.

this is the javascript

<script type="text/javascript">
function numeric(e)
{
if ((e.keyCode ==8) || (e.keyCode > 47 && e.keyCode < 58) || (e.keyCode == 110))
return;
alert('Please enter a valid value');
}
</script>

and this is inside the <body>

<li>\How much would you be willing to pay?
$<input class="blue" type="text" name="money" onkeydown="return numeric(event)"/>

</li>

this only allow the textbox to enter numeric value... you can still type in number greater than 1000.

Dormilich
04-15-2010, 01:54 PM
I’d rather go checking the input’s value.

var x = parseFloat(input.value);
if (isFinite(x) && x >= 0 && x < 1000 && (x % 1) === 0) { … }

of course you can combine both and you only have
if (parseFloat(input.value) < 1000) left

prasanthmj
04-16-2010, 08:39 AM
It will be easier to do client side validations using this JavaScript form validation script (http://www.javascript-coder.com/html-form/javascript-form-validation.phtml)

for example

frmvalidator.addValidation("money","lessthan=1000");



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum