我想要一个只允许4位数字的javascript函数,不应该允许文本字符? [英] I want a javascript function which will allow only 4 digit number and should not allow text charcter ?

查看:51
本文介绍了我想要一个只允许4位数字的javascript函数,不应该允许文本字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii,



我想要的是,我有文字框,它不应该允许我输入字符,但同时它应该允许我输入只有不应该是4位的数字



Hii ,

I want is like , I have text box and it should not allow me to enter characters but at the same time it should allow me to enter only digits which should not be 4 digits

function check(s, e) {
       debugger;
       var theEvent = e.htmlEvent || window.event;
       var key = theEvent.keyCode || theEvent.which;
       key = String.fromCharCode(key);
       var regex = /^\d{4}$/;

       if (!regex.test(key)) {
           theEvent.returnValue = false;
           if (theEvent.preventDefault)
               theEvent.preventDefault();
       }
   }





我正在使用devexpress aspx文本框..

我可以使用掩码设置,但这不适用于类型密码



I am using devexpress aspx text box ..
I can use mask settings but this is not working with type password

推荐答案

/;

if (!regex.test(key)){
theEvent.returnValue = ;
if (theEvent.preventDefault)
theEvent.preventDefault();
}
}
/; if (!regex.test(key)) { theEvent.returnValue = false; if (theEvent.preventDefault) theEvent.preventDefault(); } }





我正在使用devexpress aspx文本框..

我可以使用掩码设置,但这不适用于类型密码



I am using devexpress aspx text box ..
I can use mask settings but this is not working with type password


两个选项。



A:In html5你可以设置一个maxlength。然后你必须检查更改输入的值是什么并将其限制为4



B:在html5中你可以将文本框设置为textmode =number将输入限制为仅限数字(并在右侧添加一个数字迭代器)然后您必须检查更改的文本框以检查数字长度



I不知道为什么,但这两种方法不能一起工作。无论哪种方式,您都需要在javascript中检查输入:



快速简便:

two options.

A: In html5 you can set a maxlength. you will then have to check on change what the values entered are and restrict it to 4

B: In html5 you can set the textbox to textmode="number" which will restrict the input to numbers only (and will add a number iterator on the right) You will then have to check the textbox changed to check for the digits length

I don't know why, but these two methods don't work together. Either way you will need to check the input in javascript:

quick and easy:
<input type="text" maxlength="4" onkeyup="this.value = this.value.replace(/[^0-9]/, '')" />





PS:我建议使用jQuery更改事件,因为它包括复制粘贴以及按键



希望有帮助^ _ ^

Andy



PS: I advise using the jQuery "change" event as that covers copy-paste as well and keystrokes

Hope that helps ^_^
Andy


< dx:ASPxTextBox ID =txtPinNumberClientInstanceName =txtPinNumberCssClass =cpTextboxText =runat =server

密码=trueMaxLength =4>

< clientsideevents keyup =CheckPinNumber>





<dx:ASPxTextBox ID="txtPinNumber" ClientInstanceName="txtPinNumber" CssClass="cpTextbox" Text="" runat="server"
Password="true" MaxLength="4">
<clientsideevents keyup="CheckPinNumber">


function CheckPinNumber(s, e) {
       if (null != s.GetValue() && '' != s.GetValue()) {
           s.SetValue(s.GetValue().replace(/[^0-9]/, ''));
       }
   }


这篇关于我想要一个只允许4位数字的javascript函数,不应该允许文本字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆