开发者

how to valid only digit in textbox in firefox?

开发者 https://www.devze.com 2023-03-16 12:51 出处:网络
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 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 t开发者_如何学Cextbox, i am use asp.net too for code language.


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


Try the masked input plugin for jquery.


<textarea onkeyup="test(this);"></textarea>

and

<script type="text/javascript">
function test(el) {
el.value = el.value.replace(/[^0-9\.]/g,'');
}
</script>


<asp:RegularExpressionValidator ID="vldNumber" ControlToValidate="txtNumber" Display="Dynamic" ErrorMessage="Please Enter Numeric Value" ValidationExpression="(^([0-9]*|\d*\d{1}?\d*)$)" Runat="server"> 


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.

jsFiddle Demo

0

精彩评论

暂无评论...
验证码 换一张
取 消