views:

420

answers:

2

I'm using the onkeyup event on a form field in JavaScript, and I'm wanting to check if the key the key pressed is a numeric digit - ie. 0 - 9, so I can then do something with the input.

<input type="text" onkeyup="" />

Would I need to use Regex for this?

Thanks

+3  A: 

See these:

karim79
+1 for redirecting the user to earlier stackoverflow entries.
ChristopheD
Except the third one is this very article....
wows
@wows - hehe, sorry, removed it.
karim79
A: 

See this:

http://www.javascriptkit.com/javatutors/javascriptkey2.shtml

<script type="text/javascript">
function displayunicode(e){
var unicode=e.keyCode? e.keyCode : e.charCode
if unicode >= 48 && unicode <= 57 {
    alert('number')
}
else{
}
    alert('nonnumber')
}
</script>
<form>
<input type="text" size="2" maxlength="1" onkeyup="displayunicode(event); this.select()" />
</form>
madcolor