2016-11-15 148 views
0

我在同一頁中有兩種形式。 jquery.validate插件正在被動態加載。由於它是動態加載的,所以當用戶點擊提交按鈕時會創建驗證規則。驗證似乎在第一個表單上工作正常(表單ID = form),但不適用於第二個表單(表單ID = #form1)。多種形式的jquery驗證錯誤

我收到以下錯誤: this[0] is undefinedThis question has some solutions.但是,由於我的腳本是動態加載的,因此解決方案無法正常工作。

此外,大部分腳本都是純javascript,因爲我幾乎爲所有瀏覽器提供了支持,而且我無法控制表單元素名稱。如何進行第二次表單驗證?謝謝。

<form id="form"> 
    <input type="text" name="Please%20provide%20name." /> 
    <input type="text" name="Please%20provide%20email." /> 
    <input type="text" name="%50%6C%65%61%73%65%20%70%72%6F%76%69%64%65%20%70%68%6F%6E%65%20%6E%75%6D%62%65%72%2E" data-parsley-required /> 
    <input type="submit" value="Submit" /> 
</form> 
<form id="form1"> 
    <input type="text" name="name" /> 
    <input type="text" name="email" /> 
    <input type="text" name="phone" /> 
    <input type="submit" value="Submit" /> 
</form> 
"use strict"; 
document.addEventListener("DOMContentLoaded", function() { 
    // on load 
    var forms_count = document.forms.length, 
     i = 0; 
    // find all forms in the page 
    for (i; i < forms_count; i++) { 
     // for each form 
     var form = document.forms[i]; 
     if (form.addEventListener) { 
      // add submit handler to validate 
      form.addEventListener("submit", validateForm, false); //Modern browsers 
     } else if (form.attachEvent) { 
      form.attachEvent('onsubmit', validateForm); //Old IE < 9 
     } 
    } 
    // add scripts 
    var script = document.createElement('script'); 
    script.setAttribute('type', 'text/javascript'); 
    script.setAttribute('src', "https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"); 
    document.head.appendChild(script); 

    // add validation rules 
    validationClasses(); 
}); 

function validationClasses() { 
    var forms_count = document.forms.length, 
     i = 0; 
    // find all forms in the page 
    for (i; i < forms_count; i++) { 
     // for each form 
     var form = document.forms[i]; 
     // get form elements 
     var elements = document.getElementById(form.id).elements; 
     for (var element of elements) { 
      // for each element, check name field and add appropriate validation class. 
      var decode_name = decodeURIComponent(element.name); 
      if (decode_name.toLowerCase().match(/(name|first name)/)) { 
       // matching for name field. 
       element.className += " valid_name"; 
      } 
      if (decode_name.toLowerCase().match(/(email|email address)/)) { 
       // matching for email field. 
       element.className += " valid_email"; 
      } 
      if (decode_name.toLowerCase().match(/(phone|phone number)/)) { 
       // matching for phone field. 
       element.className += " valid_phone"; 
      } 
     } 
    } 
} 

function validationJQRules() { 
    // custom rules 
    $.validator.addMethod("mobilenumber", function(value, element) { 
     return this.optional(element) || /^[0-9-+\s]+$/.test(value); 
    }, "Invalid Number"); 

    // Define validate form rules in jquery validate 
    jQuery.validator.addClassRules("valid_name", { 
     required: true, 
    }); 
    jQuery.validator.addClassRules("valid_email", { 
     required: true, 
     email: true 
    }); 
    jQuery.validator.addClassRules("valid_phone", { 
     required: true, 
     mobilenumber: true 
    }); 
} 

function validateForm(event) { 
    validationJQRules(); 
    var form = this, 
     valid = false; 

    if (!valid) { 
     // stop further execution of the event if not valid. 
     event.preventDefault(); 
     event.stopPropagation(); 
     event.stopImmediatePropagation(); 
    } else { 
     console.log("proceed with form submit."); 
    } 

    console.log(form.id); 
    $(form.id).valid(); 
    console.log($(form.id).valid()); 
}; 

另外,用於與注射驗證腳本執行驗證的任何其它替代方法是非常讚賞。

+0

爲什麼插入jQuery vaiidation並添加onsubmit處理程序?做一個或另一個 – mplungjan

+0

你會得到什麼錯誤? –

+0

好吧,目前我正在使用jQuery驗證,可以稍後更改爲其他插件。所有腳本將被注入到可能有或沒有多種形式的外部網頁中。外部網頁可以不斷變化。 – Sp0T

回答

0

$(form.id).valid()可能是一個問題。 使用

$("#" + form.id).valid(); 
console.log($("#" + form.id).valid());