2010-07-25 40 views

回答

3

您可以使用.addMethod()將自定義驗證方法添加到驗證插件。

http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage


更新:

下面是一個例子,你可以測試:http://jsfiddle.net/W8EsU/

HTML

<form id='theForm'> 
    <input id='test_field' name='test_field' value='jQuery' /> 
    <br> 
    <input id='test_field2' name='test_field2' value='prototypejs' /> 
</form>​ 

jQuery

// Add a validation method to the validator plugin 
    // that can be applied as a rule to whatever fields 
    // you want. That way you get your custom validation 
    // integrated into the functionality of the plugin 
$.validator.addMethod(
    "mustIncludejQuery", 
    function(value, element) { 
     return value.toLowerCase().indexOf('jquery') > -1; 
    }, 
    "You must type jQuery to be valid." 
); 

    // Apply the custom validation to the fields 
$('#theForm').validate({ 
    rules: { 
     test_field:'mustIncludejQuery', 
     test_field2:'mustIncludejQuery' 
    } 
}); 

    // Demonstrates that they will be executed 
    // like any other validation rule. 
$('#theForm').valid(); 
​ 
+0

+1 - 這真的是要走的路,將它作爲規則添加到您檢查的任何元素。 – 2010-07-25 15:19:23