javascript HTML 文本框的 Aadhar 数字格式验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47428101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Aadhar Number Format Validation for HTML Textbox
提问by Vinay
I want aadhar number in format of xxxx-xxxx-xxxx. Text box sholud take the input in this format automatically.
我想要 xxxx-xxxx-xxxx 格式的 aadhar 号码。文本框应该自动采用这种格式的输入。
While entering the input the input should automatically convert into this(xxxx-xxxx-xxxx) format.
在输入输入时,输入应自动转换为这种(xxxx-xxxx-xxxx)格式。
Only numericals should be accepted.
只应接受数字。
I want mobile number in format +91(or any other country code)-9999999999(10 digit mobile number).
我想要格式为 +91(或任何其他国家/地区代码)-9999999999(10 位手机号码)的手机号码。
I have tried /^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/, but its not working please help
我试过了/^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/,但它不起作用,请帮忙
回答by gurvinder372
I want aadhar number in format of xxxx-xxxx-xxxx. text box sholud take the input in this format automatically.
我想要 xxxx-xxxx-xxxx 格式的 aadhar 号码。文本框应该自动采用这种格式的输入。
One example would be to enforce input to only accept the adhaar number format
一个例子是强制输入只接受 adhaar 数字格式
$('[data-type="adhaar-number"]').keyup(function() {
var value = $(this).val();
value = value.replace(/\D/g, "").split(/(?:([\d]{4}))/g).filter(s => s.length > 0).join("-");
$(this).val(value);
});
$('[data-type="adhaar-number"]').on("change, blur", function() {
var value = $(this).val();
var maxLength = $(this).attr("maxLength");
if (value.length != maxLength) {
$(this).addClass("highlight-error");
} else {
$(this).removeClass("highlight-error");
}
});
.highlight-error {
border-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" data-type="adhaar-number" maxLength="19">
回答by Rajkumar Somasundaram
I am not interested in giving u everything, but I will point you in the right direction. Use regular expressions.
我对给你一切都不感兴趣,但我会为你指明正确的方向。使用正则表达式。
This regex will help your case but you need to put in your effort on how it must be used in your situation.
此正则表达式将有助于您的情况,但您需要努力了解如何在您的情况下使用它。
console.log(/\d{4}-\d{4}-\d{4}-\d{4}/.test('1000-1000-1000-1002'));

