PDA

Click to See Complete Forum and Search --> : Allow only numbers !!!


bhaskerg
Mar 15th, 2001, 08:09 PM
In javascript how to check for a textbox to allow only numbers to be typed and not to allow characters and special characters like ' " etc...

sail3005
Mar 15th, 2001, 08:31 PM
all single digit (pos, real) numbers are either greater than or equal to zero, or less than or equal to 9. So just run through the string and look for something that doesn't fit this description.



for (i=0; i<passedValue.length; i++) {
if (passedVal.charAt(i) < "0") { return false}
if (passedVal.charAt(i) > "9") { return false}
}

Mark Sreeves
Mar 16th, 2001, 12:07 AM
or like this:


<HTML>
<HEAD>
<script Language="JavaScript">
function check(contents)
{
if (((contents / contents) != 1) && (contents != 0))
{
alert('Please enter only numbers into this text box')
}
}

</script>
</HEAD>
<BODY>

<input name="txt1" onBlur="check(this.value)">
</BODY>
</HTML>

John Slade
Apr 14th, 2001, 06:51 AM
Or you could be really flash and use regular expressions to do it:-



<HTML>
<HEAD>
<script Language="JavaScript">
function check(contents)
{
var pattern = /[0-9]/;

flag = pattern.test(contents);

if (!flag) {

alert('Please enter only numbers into this text box')

}
}

</script>
</HEAD>
<BODY>

<input name="txt1" onBlur="check(this.value)">
</BODY>
</HTML>