简体   繁体   English

需要编号验证正则表达式

[英]Need Number validation regular expression

The following point must be considered before creating a regular expression: 在创建正则表达式之前必须考虑以下几点:

Add +(plus) before Every number that user cant delete. 在用户无法删除的每个数字之前加上+(加号)。

number length min 2 max 20 号码长度最小值2最大值20

only number,backspace, Arrowkey allow 仅数字,退格键,方向键允许

Right now I'm using this: 现在我正在使用这个:

 function Validatenumber(event) { var regex = new RegExp("^[0-9?.*+*]+$"); var key = String.fromCharCode(event.charCode ? event.which : event.charCode); if (regex.test(key) || event.keyCode == 8) { return true; } else{ event.preventDefault(); return false; } } 

Try this(Got this from my project). 试试这个(从我的项目中得到这个)。

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

Try below code 试试下面的代码

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1    /jquery.min.js"></script>
<script>
function isNumber(evt) {
evt = (evt) ? evt : window.evt;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 37 && charCode != 39) {
    return false;
}
return true;
}
</script>
</head>
<body>

Enter your name: <input type="text" onkeypress="return isNumber(event);">

</body>
</html>

I got Right Answer , i know im fresher and and i don't have enough knowledge but if u have than share with any one instead of finding mistake in Question , why u use this tag and blah blah ... 我得到了正确答案,我知道较新,并且我没有足够的知识,但是如果您比不与任何人分享而不是在Question中发现错误,为什么您要使用此标记等等?

https://github.com/stripe/jquery.mobilePhoneNumber https://github.com/stripe/jquery.mobilePhoneNumber

Check out this link for perfect answer of my question... 看看这个链接,我的问题的完美答案...

and thank You very much friends who answer my question or try to answer my question .. keep it up friends... 并非常感谢您回答我的问题或尝试回答我的问题的朋友..让它成为朋友...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM