|
-
Mar 15th, 2001, 09:09 PM
#1
Thread Starter
Lively Member
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...
-
Mar 15th, 2001, 09:31 PM
#2
PowerPoster
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.
Code:
for (i=0; i<passedValue.length; i++) {
if (passedVal.charAt(i) < "0") { return false}
if (passedVal.charAt(i) > "9") { return false}
}
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
USAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSAUSA
-
Mar 16th, 2001, 01:07 AM
#3
Frenzied Member
or like this:
Code:
<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>
-
Apr 14th, 2001, 06:51 AM
#4
Or you could be really flash and use regular expressions to do it:-
Code:
<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>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|