首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >身份证号码格式不起作用的jQuery中的RegEx模式

身份证号码格式不起作用的jQuery中的RegEx模式
EN

Stack Overflow用户
提问于 2012-08-16 17:19:09
回答 1查看 1.8K关注 0票数 0

我正在尝试在我的申请中验证国民身份证号码。但它不起作用。它为所有的值抛出错误。即使我输入了正确的格式。我的格式应该是123456789v或123456789x。我在这里添加了我的jQuery验证。有谁能帮帮我吗?如果你需要更多的细节,请评论它。

代码语言:javascript
复制
$('.btn-submit').click(function(e) {

  required = ["employeeName","gender","employeeId","nic","firstName","lastName","departmentId","designation","address"];
  // Declare the function variables:
  // Parent form, form URL, email regex and the error HTML
  var $formId = $(this).parents('form');
  var formAction = $formId.attr('action');
  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  var nicReg=/^[0-9]{9}[vVxX]$/;
  var $error = $('<span class="error"></span>');

  // Prepare the form for validation - remove previous errors
  $('li',$formId).removeClass('error');

  $('span.error').remove();

  // Validate all inputs with the class "required"
  $('.required',$formId).each(function(){
    for (i=0;i<required.length;i++){
      var inputVal = $('#'+required[i]).val();
      var $parentTag = $('#'+required[i]).parent();
      if(inputVal == ''){
        $parentTag.addClass('error').append($error.clone().text('Required Field'));
      }

    }

    // Run the email validation using the regex for those input items also having class "email"
    if($(this).hasClass('nic') == true){
      var $parentTag2=$('#nic').parent();
      if(!nicReg.test(inputVal)){
        $parentTag2.addClass('error').append($error.clone().text('Enter valid NIC'));
      }
    }

    // Check passwords match for inputs with class "password"
    if($(this).hasClass('password') == true){
      var password1 = $('#password-1').val();
      var password2 = $('#password-2').val();
      if(password1 != password2){
        $parentTag.addClass('error').append($error.clone().text('Passwords must match'));
      }
    }

  });

  // All validation complete - Check if any errors exist
  // If has errors

  if ($('span.error').length > 0) {

    $('span.error').each(function(){

      // Set the distance for the error animation
      var distance = 5;

      // Get the error dimensions
      var width = $(this).outerWidth();

      // Calculate starting position
      var start = width + distance;

      // Set the initial CSS
      $(this).show().css({
        display: 'block',
        opacity: 0,
        right: -start+'px'
      })
      // Animate the error message
      .animate({
        right: -width+'px',
        opacity: 1
      }, 'slow');

    });
  } else {
    this.form.action = "DepartmentServlet?method=add";
    $formId.submit();

  }
  // Prevent form submission
  e.preventDefault();

});

// Fade out error message when input field gains focus
$('.required').focus(function(){
  var $parent = $(this).parent();
  $parent.removeClass('error');
  $('span.error',$parent).fadeOut();
});
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-16 17:27:43

乍一看,"inputVal“只在"for”循环中设置。它的最后一个值是在nicReg.test()中测试的

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11984118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档