i have problem to valid digit in textbox in IE 9 and firefox 4 & 5, is any one know ? i try all previous question's answer but still face problem, i just want use to enter only digit in textbox, i am use asp.net too for code language.
-
Duplicate. http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input – Alterlife Jul 01 '11 at 10:13
-
i know but that code is not working in IE 9 and Firefox 4 & 5 – Bhargav Mistri Jul 01 '11 at 10:20
-
@Bhargav Which code? There are several solutions there. Closing as duplicate. – kapa Jul 01 '11 at 10:26
-
i used all the code but no one working in firefox 4 & 5 – Bhargav Mistri Jul 01 '11 at 10:28
-
if code not work its not right solution – Bhargav Mistri Jul 01 '11 at 10:29
-
window.event is not working in firefox 4 & 5 – Bhargav Mistri Jul 01 '11 at 10:32
-
@Bhargav I tested [this solution](http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input/469419#469419) and it works fine in IE9 and FF5: [jsFiddle](http://jsfiddle.net/ZfyVc/1/). The problem might be PEBKAC. Btw, I wrote a simple jQuery solution for you. – kapa Jul 01 '11 at 10:49
5 Answers
As far as I've understood you want to allow only numeric characters in the text field and you will return other characters if they are not numeric. So, you may go through this
step1. create a javascript function
<script language="text/javascript">
function onlyNumbers(evt)
{
var e = window.event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
step2. onkeypress call the function like this
If you call any javascript method on any event the first parameter always passed is event. For IE you need to catch the event by window.event.
For more clear ideas you can visit:- https://developer.mozilla.org/En/DOM/Event

- 1,065
- 4
- 14
- 27
<textarea onkeyup="test(this);"></textarea>
and
<script type="text/javascript">
function test(el) {
el.value = el.value.replace(/[^0-9\.]/g,'');
}
</script>

- 708
- 4
- 4
-
That will allow a fullstop - that is not what the user wants. use /[^0-9]/g. – Michael Wright Jul 01 '11 at 10:17
-
i already use this code but my TL said me they don't want any thing to show in textbox – Bhargav Mistri Jul 01 '11 at 10:22
<asp:RegularExpressionValidator ID="vldNumber" ControlToValidate="txtNumber" Display="Dynamic" ErrorMessage="Please Enter Numeric Value" ValidationExpression="(^([0-9]*|\d*\d{1}?\d*)$)" Runat="server">

- 5,661
- 2
- 26
- 32
Using jQuery, it is quite easy to implement.
You should mark your inputs that need to be numeric only with a class (inline event handlers are mostly useless if you have jQuery anyways):
<input type="text" class="numericInput">
and use this little jQuery:
$('input.numericInput').keypress(function (e) {
return /[0-9]/.test(String.fromCharCode(e.which));
});
Tested: Chrome, FF4, IE9. If you want to allow other characters, just manipulate the regex. You might want to allow some other keys, like Backspace.

- 77,694
- 21
- 158
- 175
-
@Bhargav Then how come it works? I promise I'm not a magician. [Firefox supports it](https://developer.mozilla.org/en/dom/event.which), just like IE9, but jQuery normalizes `event.which` anyways, so it will work. – kapa Jul 01 '11 at 11:01