【问题标题】:How can I define a Validator function in Javascript (Jquery) to validate an input field?如何在 Javascript (Jquery) 中定义验证器函数来验证输入字段?
【发布时间】:2016-10-10 23:39:18
【问题描述】:

我想定义一个函数来验证输入字段是否是有效的个人 ID!我有你所看到的功能:

function checkMeliCode(code)
{
    if(!/^\d{8,10}$/.test(code) || /^(0{8,10}|1{8,10}|2{8,10}|3{8,10}|4{8,10}|5{8,10}|6{8,10}|7{8,10}|8{8,10}|9{8,10})$/.test(code))
        return false;
    var L=code.length, _=0;
    for(i=0;i<L-1;i++)
        _+=code.charAt(i)*(L-i);
    _%=11;
    return (code.charAt(L-1)==((_<2)?_:11-_))
}

我正在使用 On Rails 上的向导表单。这是我在查看的 JavaScript 代码:

$(function() {

$("#wizard").steps();
$("#form").steps({
  bodyTag: "fieldset",
  onStepChanging: function (event, currentIndex, newIndex) {
      // Always allow going backward even if the current step contains invalid fields!
      if (currentIndex > newIndex) {
          return true;
      }

      // Forbid suppressing "Warning" step if the user is to young
      if (newIndex === 3 && Number($("#age").val()) < 18) {
          return false;
      }

      var form = $(this);

      // Clean up if user went backward before
      if (currentIndex < newIndex) {
          // To remove error styles
          $(".body:eq(" + newIndex + ") label.error", form).remove();
          $(".body:eq(" + newIndex + ") .error", form).removeClass("error");
      }
      // Disable validation on fields that are disabled or hidden.
      form.validate().settings.ignore = ":disabled,:hidden";

      // Start validation; Prevent going forward if false
      return form.valid();
  },
  onStepChanged: function (event, currentIndex, priorIndex) {
      // Suppress (skip) "Warning" step if the user is old enough.
      if (currentIndex === 2 && Number($("#age").val()) >= 18) {
          $(this).steps("next");
      }

      // Suppress (skip) "Warning" step if the user is old enough and wants to the previous step.
      if (currentIndex === 2 && priorIndex === 3) {
          $(this).steps("previous");
      }
      switch (priorIndex){
        case 0:
          console.log("my log");
          send_basic_info();
          break;
        // case 1:
        //   send_education();
        //   break;
        // case 2:
        //   send_experience();
        //   break;
        // case 3:
        //   send_laguages();
        //   break;
        // case 4:
        //   send_computer_skill();
        //   break;
        // case 5:
        //   send_computer_certificate();
        //   break;

      }


  },
  onFinishing: function (event, currentIndex) {
      var form = $(this);

      // Disable validation on fields that are disabled.
      // At this point it's recommended to do an overall check (mean ignoring only disabled fields)
      form.validate().settings.ignore = ":disabled";

      // Start validation; Prevent form submission if false
      return form.valid();
  },
  onFinished: function (event, currentIndex) {
      var form = $(this);

      // Submit form input
      // form.submit();
      $.ajax({
        url: "/resume/finished",
        data: null,
        dataType: "json",
        type: "post",
        success: function(data){
          document.location.href="/";
        }
      });
  }
}).validate({
          errorPlacement: function (error, element) {
              element.before(error);
          },
          rules: {
              confirm: {
                  equalTo: "#password"
              }
          }
      });
});

这是我希望验证的输入:

<div>
   <label>*National ID</label>
   <input name="bi-national-id" type="text" placeholder="Enter your National ID" id="national_id" class="form-control required" ><br>
</div>

问题是我真的不知道 .validate() 和 .validation() 之间的区别。资产已添加(表单验证和验证)。

【问题讨论】:

    标签: javascript jquery ruby-on-rails validation


    【解决方案1】:

    我有答案享受它:)

       $("#form").steps({ .... }).formValidation({
          framework: 'bootstrap',
                icon: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
    
                    bi_national_id: {
                        validators: {
                              callback: {
                                message: 'Enter a Valid National ID',
                                callback: function       isValidIranianNationalCode(input) {
                                     if (!/^\d{10}$/.test(input))
                                     return false;
                                var check = parseInt(input[9]);
                                var sum = [0, 1, 2, 3, 4, 5, 6, 7, 8]
                               .map(function (x) { return parseInt(input[x]) * (10 - x); })
                               .reduce(function (x, y) { return x + y; }) % 11;
                               return sum < 2 && check == sum || sum >= 2 && check + sum == 11;
                                    }
                              } 
                        }
                    },
    
                }
            })
    

    我使用了 FormValidation。更多信息和示例请点击here

    断腿

    【讨论】:

      猜你喜欢
      • 2021-12-11
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 2011-04-25
      • 2017-09-11
      相关资源
      最近更新 更多