最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Aadhar Number Format Validation for HTML Textbox - Stack Overflow

programmeradmin1浏览0评论

I want aadhar number in format of xxxx-xxxx-xxxx. Text box sholud take the input in this format automatically.

While entering the input the input should automatically convert into this(xxxx-xxxx-xxxx) format.

Only numericals should be accepted.

I want mobile number in format +91(or any other country code)-9999999999(10 digit mobile number).

I have tried /^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/, but its not working please help

I want aadhar number in format of xxxx-xxxx-xxxx. Text box sholud take the input in this format automatically.

While entering the input the input should automatically convert into this(xxxx-xxxx-xxxx) format.

Only numericals should be accepted.

I want mobile number in format +91(or any other country code)-9999999999(10 digit mobile number).

I have tried /^(?:\(\d{3}\)|\d{3}-)\d{3}-\d{4}$/, but its not working please help

Share Improve this question edited Dec 13, 2017 at 22:12 chrki 6,3336 gold badges38 silver badges59 bronze badges asked Nov 22, 2017 at 6:24 VinayVinay 11 gold badge1 silver badge2 bronze badges 3
  • 1 Please share what you have tried with a working snippet using <> to demonstrate your issue. – gurvinder372 Commented Nov 22, 2017 at 6:25
  • Please post some of your code attempts so people can try to help you. We will not code this for you but we will provide help with your code. – Mark Commented Nov 22, 2017 at 6:25
  • /^(?:\\d{4})|\d{4}-\d{4}-\d{4}$/ – R.K.Bhardwaj Commented Nov 22, 2017 at 6:30
Add a ment  | 

2 Answers 2

Reset to default 0

I want aadhar number in format of xxxx-xxxx-xxxx. text box sholud take the input in this format automatically.

One example would be to enforce input to only accept the adhaar number format

$('[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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" data-type="adhaar-number" maxLength="19">

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'));

发布评论

评论列表(0)

  1. 暂无评论