如何在发送之前包含Ajax验证程序

如何在发送之前包含Ajax验证程序,ajax,validation,Ajax,Validation,我设法使这段代码提交了一个表单,它工作得很好,但我无法实现一个验证器,不需要它,但它显示没有消息只是不发送表单是空的 我搜索了一下,但还是做不好。有人能帮我吗 <script type="text/javascript"> jQuery(document).ready(function(){ jQuery('#newsletter').submit(function(){ var dado

我设法使这段代码提交了一个表单,它工作得很好,但我无法实现一个验证器,不需要它,但它显示没有消息只是不发送表单是空的

我搜索了一下,但还是做不好。有人能帮我吗

        <script type="text/javascript">
        jQuery(document).ready(function(){
            jQuery('#newsletter').submit(function(){
                    var dados = jQuery( this ).serialize();

                    jQuery.ajax({
                        type: "POST",
                        url: "newsletter_cadastrar.asp?f=1",
                        data: dados,
                        success: function( data )
                        {
                            $('#resultado-form-newsletter').html(data);
                        }
                    });

                    return false;
                });
        });
        </script>

<input id="nomenewslbase" name="nomenewslbase" type="text">
<input id="emailnewslbase" name="emailnewslbase" type="email">

感谢您的关注。

如果您希望应用jquery验证规则,那么您可以这样做:

<script type="text/javascript">
    function ValidationRules() {
}

ValidationRules.prototype.initializeSaveValidators = function() {
    jQuery.validator.addMethod("csProjectName",
        function(value, element) {
            if ($.trim($('#txtProjectName').val()) == "") {
                if ($.trim($(element).val()) != '')
                    return true;
                else
                    return false;
            } else
                return true;
        }, "Project Name is required");

    jQuery.validator.addMethod("csDescription",
    function (value, element) {
        if ($.trim($('#txtDescription').val()) == "") {
            if ($.trim($(element).val()) != '')
                return true;
            else
                return false;
        }
        else
            return true;
    }, "Description is required");
};

ValidationRules.prototype.enableSaveValidators = function () {
    jQuery.validator.classRuleSettings.csProjectName = { csProjectName: true };
    jQuery.validator.classRuleSettings.csDescription = { csDescription: true };
};

ValidationRules.prototype.disableSaveValidators = function () {
    jQuery.validator.classRuleSettings.csProjectName = { csProjectName: false };
    jQuery.validator.classRuleSettings.csDescription = { csDescription: false };
};
jQuery(document).ready(function () {        
    var validationRules = new ValidationRules();
    validationRules.initializeSaveValidators();   
    validationRules.enableSaveValidators();      
    $("#btnsubmit").click(function () {                  
        applyjQueryValidation();
        return false;
    });
});
function applyjQueryValidation() {
        var isValid = $('#newsletter').valid();
        //here you can manually check for some extra validations
        if (isValid==true) {
            alert("valid");
            //here is you ajax call
        }else{
            alert("invalid");
        }      
}
</script>
此外,您还需要包括


以下是工作示例。

亲爱的Sanjeev Rai。谢谢,但是你的代码不起作用。你能再帮我一次吗?之前的代码中没有初始化Jquery验证器。现在,我已经编辑了代码,您可以再试一次!如果它不起作用,那么你能在评论中发布错误吗?它还没有起作用。当我不发送时,它将被发送为空。这可能会有帮助!