当我向Jquery Validate表单添加远程无效时提交

时间:2014-01-11 11:39:09

标签: ajax jquery jquery-validate

当我向jquery添加远程验证时,我的表单提交即使有错误,当我删除远程它正常工作,你不能提交表单,除非你已填写所有字段?有任何想法吗?代码在下面

<script>
$(document).ready(function () {

    $("#SignUp").validate({
        onsubmit: true,
        onkeyup: function(element) { $(element).valid(); },
        rules: {
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                password: true
            },
            confirm_password: {
                equalTo: "#password",
                required: true
            },
            company: {
                nls:true,
                required: true
            },
            telephone: {
                required: true,
                phoneUK: true
            },
            email2: {
                email: true
            },
            website: {
                url: true
            },
            address1: {
                nls:true
            },
                address2: {
                nls:true
            },
            town: {
                nls:true    
            },
            postcode: {
                postcodeUK:true
            },
            country: {
                selectcountry:true
            },
            terms:{
                required:true               
            },
            answer:{ 
                remote: "Captcha/check-captcha.php",
                type: "POST",
                data: {
                        captcha: function() {
                        return $("#answer").val();
                                }
                        }

            }
        },
        messages:{
            email:
                    {
                        required: "Please Enter an Email Address"
                    },
            password:
                    {
                        required: "Please Enter a Password"
                    },
            confirm_password:
                    {
                        required: "Please Confirm Your Password"
                    },
            company:
                    {
                        required: "Please Enter Your Company/Climbing Gym Name"
                    },
            telephone:
                    {
                        required: "Please Enter a Telephone Number"
                    },
            terms:
                    {
                        required: "Please Agree Our Terms and Conditions"
                    },
            answer:{
                        remote: "You Have Entered The Captcha Correctly When This Message Disappears"
                    }

        }
    });

    $.validator.addMethod("password", function (value, element) {
        return this.optional(element) || /^[A-Za-z0-9!@#$%^&*()_]{6,16}$/i.test(value);
    }, "Passwords are 6-16 characters");

    $.validator.addMethod("nls", function(value, element)
    {
    return this.optional(element) || /^[a-zA-Z0-9\s.\-_']+$/i.test(value);
    }, "Please Only Enter Alpha Numeric Characters and Spaces"); 


    jQuery.validator.addMethod('selectcountry', function (value) {
        return (value != 'Nothing');
    }, "Please Select a Country");

    $.validator.addMethod("url", function(value, element)
    {
    return this.optional(element) || /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/i.test(value);
    }, "Please Enter A Valid Website URL");





});
</script>

PHP如下:

<?php
session_start();
if(strtolower($_REQUEST['answer']) == $_SESSION['captcha']){    
echo 'true';die;
}else{
 echo 'false';die;

}
?>

验证仍然适用于带有和没有遥控器的键盘等,但只是没有提交?

1 个答案:

答案 0 :(得分:0)

远程规则语法略有不同

answer: {
    required: true,
    remote: {
        url: "Captcha/check-captcha.php",
        type: "POST",
        data: {
            captcha: function () {
                return $("#answer").val();
            }
        }
    }
}