2015-02-06 151 views
0

我想驗證我的表單提交使用jqueryvalidation.js。jQuery驗證無法正常工作

當我使用這個代碼這段時間的驗證只能火狐

jQuery.validator.setDefaults({ 

    if(success) 
    { 
     $("#addbusiness").submit(); 
    } 
}); 
$("#addbusiness").validate({ 
    rules: { 
     phone: { 
      required: true, 
      phoneUS: true 
     } 
    } 
}); 

如果我用這個代碼,然後驗證不工作的所有瀏覽器

jQuery.validator.setDefaults({ 


    if($("#addevent").valid()) 
{ 
    $("#addevent").submit(); 
} 
}); 
$("#addevent").validate({ 
    rules: { 
     phone: { 
      required: true, 
      phoneUS: true 
     } 
    } 
}); 

我想,如果我的表格無效,則形成無法提交。

+0

請參考[SO標籤Wiki頁面](http://stackoverflow.com/tags/jquery-validate/info)的基本用法插件和更多信息的鏈接。 – Sparky 2015-02-06 16:11:49

回答

2

你的這部分代碼...

jQuery.validator.setDefaults({ 
    if (success) { 
     $("#addbusiness").submit(); 
    } 
}); 

.setDefaults()方法只能接受一個對象字面(由key:valueoptions as per the author)。如果不打破方法和可能的整個插件,就不能在其中放置JavaScript條件。即使作爲一個功能,反正也行不通,因爲success在這個上下文中沒有被定義爲任何東西。

我想如果我的表單無效然後表單無法提交。

如果允許表單提交,那麼使用jQuery驗證插件就沒有意義了。 默認情況下,適當配置時,它會阻止,直到提交表單有效 ...

$(document).ready(function() { 

    $("#addevent").validate({ 
     rules: { 
      phone: { 
       required: true, 
       phoneUS: true 
      } 
     } 
    }); 

}); 

HTML

<form id="addevent"> 
    <input type="text" name="phone" /> 
    <input type="submit" /> 
</form> 

DEMO:http://jsfiddle.net/cLqh4j2v/