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

javascript - Regular expression to validate National Identity Card number which has 9 digits with final digit being X or V - Sta

programmeradmin3浏览0评论

function PassportValidate(s, e) {
  var option = rbIdentityVerification.GetValue().toString(); //gets the selected option which is NIC selected by default. devexpress radiobuttonlist
  var text = e.value.toString(); //gets the value from the textbox

  if (option == "NIC") {
    var pattern2 = RegExp("^\d{9}(X|V)$");

    if (!pattern2.test(text)) {
      e.isValid = false;
      e.errorText = "Passport number invalid.(Max 9 numbers)"; //Error message that is to be shown
    }
  }
}

function PassportValidate(s, e) {
  var option = rbIdentityVerification.GetValue().toString(); //gets the selected option which is NIC selected by default. devexpress radiobuttonlist
  var text = e.value.toString(); //gets the value from the textbox

  if (option == "NIC") {
    var pattern2 = RegExp("^\d{9}(X|V)$");

    if (!pattern2.test(text)) {
      e.isValid = false;
      e.errorText = "Passport number invalid.(Max 9 numbers)"; //Error message that is to be shown
    }
  }
}

Regular expression to validate National Identity Card number which has 9 digits with final digit being X or V. the code always return invalid, so even a correct NIC is entered still returns as invalid

Share Improve this question edited Dec 5, 2015 at 9:12 Bartłomiej Semańczyk 62k52 gold badges250 silver badges401 bronze badges asked Dec 3, 2015 at 8:24 Lourdes AdheeshaLourdes Adheesha 311 silver badge2 bronze badges 1
  • 1 please ignore the error text... – Lourdes Adheesha Commented Dec 3, 2015 at 8:25
Add a ment  | 

3 Answers 3

Reset to default 2

Though \d is modern regex shorthand for [0-9], I prefer to use latter.

This regex work fine ^([0-9]{9})(X|V)$.

Here is the working jsfiddle.

Try using This (Sri Lanka NIC new and old formats). This will accept all the numbers with V or X (Upper or lower).

/^([0-9]{9})(X|V)$|^([0-9]{11})/gis

I think this may will help you

^\d{9}[V|v|X|x]$

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论