如何使用.submit和.validity

时间:2013-10-02 14:01:02

标签: jquery validity.js

我正在使用.validity jquery插件(validity.thatscaptaintoyou.com/),并尝试使用.submit,但我不确定如何防止两者同时执行。基本上,除非有效性通过,否则我不想提交执行。

有效性检查:

$("#myform").validity(function() {
    $("#first_name").require();
    $("#last_name").require();
    $("#email").require().match('email', 'Please enter a valid e-mail address');
});

示例.submit

$('#myform').submit(function(event) {//Execute only if validity passed}

2 个答案:

答案 0 :(得分:0)

您可以尝试使用自定义模式    (函数(){

// We'll decide to install our custom output mode under the name 'custom':
 $.validity.outputs.custom = {


    end:function(results) { 


        if (results.valid) {
            $('#myform').submit(function(event) {//Execute only if validity passed});
        }

    }
}
})();

// Now enable the output mode we just installed.
$.validity.setup({ outputMode:'custom' });

答案 1 :(得分:0)

这对我有用:

      function validateMyForm() {

    // Start validation:
    $.validity.start();

    // Validator methods go here:
    $("#first_name").require();
    $("#last_name").require();
    $("#email").require().match('email', 'Please enter a valid e-mail address');

    // All of the validator methods have been called:
    // End the validation session:
    var result = $.validity.end();

    // Return whether it's okay to proceed:
    return result.valid;
}

// Submit handler for /purchase
$('#myform').submit(function(event) {

//Execute only if validity passed
        if (validateMyForm()) {
               //Submit form or do whatever

    }else{
               //Return False so form doesn't actually submit
        return false;
    }
});