2016-06-10 118 views
0

嘿傢伙:)預先感謝您把時間放在這裏:) 我想定義一個函數來驗證輸入字段是否是有效的個人ID!我具備的功能,你可以看到:如何在Javascript(Jquery)中定義驗證器函數來驗證輸入字段?

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("Fuck this shit"); 
      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()之間的區別。資產已添加(表單驗證和驗證)。

我將不勝感激,如果你能幫助我:)

回答

0

我已經得到了答案享受吧:)

$("#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

斷腿