Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在提交时选中jQuery验证之前提交表单_Jquery_Ajax_Forms_Validation_Submit - Fatal编程技术网

在提交时选中jQuery验证之前提交表单

在提交时选中jQuery验证之前提交表单,jquery,ajax,forms,validation,submit,Jquery,Ajax,Forms,Validation,Submit,我使用Ajax和jQuery来验证和提交表单中的信息。我遇到的问题是,当我单击submit按钮时,表单会在执行验证检查和显示错误之前立即将信息提交给服务器 我不确定这里有什么问题,所以任何输入都会很好 干杯 $('#submit_second').click(function() { //remove classes $('#second_step input').removeClass('error').removeClass('valid'); var emailP

我使用Ajax和jQuery来验证和提交表单中的信息。我遇到的问题是,当我单击submit按钮时,表单会在执行验证检查和显示错误之前立即将信息提交给服务器

我不确定这里有什么问题,所以任何输入都会很好

干杯

$('#submit_second').click(function() {
    //remove classes
    $('#second_step input').removeClass('error').removeClass('valid');

    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    var phonePattern = /^\+?[0-9]{0,15}$/ ;  
    var fields = $('#second_step input[type=text]');
    var error = 0;
    fields.each(function() {
        var value = $(this).val();
        if( value.length<1 || value==field_values[$(this).attr('id')] || ( $(this).attr('id')=='email' && !emailPattern.test(value))) {
            $(this).addClass('error');
            $(this).effect("shake", { times:3 }, 50);

            error++;
        } else {
            $(this).addClass('valid');
        }
        if( value.length<1 || value==field_values[$(this).attr('id')] || ( $(this).attr('id')=='phone' && !phonePattern.test(value) )  ) {
            $(this).addClass('error');
            $(this).effect("shake", { times:3 }, 50);

            error++;
        } else {
            $(this).addClass('valid');
        }

    });

    if(!error) {
        //update progress bar
        $('#progress_text').html('66% Complete');
        $('#progress').css('width','226px');

        //slide steps
        $('#second_step').slideUp();
        $('#fourth_step').slideDown();     
    } else return false;
});

$('#submit_second').click(function(){

    url =$("input#url").val();
    yourname =$("input#yourname").val();
    email =$("input#email").val();
    phone =$("input#phone").val();

    //send information to server
    var dataString = 'url='+ url + '&yourname=' + yourname + '&email=' + email + '&phone=' + phone;  

    alert (dataString);

    $.ajax({  
        type: "POST",  
        url: "#",  
        data: "url="+url+"&yourname="+yourname+"&email="+email+'&phone=' + phone,
        cache: false,
        success: function(data) {  
            console.log("form submitted");
            alert("success");
        }
    });  
    return false;
});
$('submit#u second')。单击(函数(){
//删除类
$(“#第二步输入”).removeClass('error').removeClass('valid');
var emailPattern=/^[a-zA-Z0-9.-]+@[a-zA-Z0-9.-]+\[a-zA-Z]{2,4}$/;
var phonePattern=/^\+?[0-9]{0,15}$/;
变量字段=$(“#第二步输入[type=text]”);
var误差=0;
字段。每个(函数(){
var值=$(this.val();

如果(value.length我想你要找的是这样的东西:

$('#submit_second').submit(function(evt){
    evt.preventDefault();
    ...
});

查看jQuery文档:

我尝试了这一点,但表单中有多个部分是动态加载的,在每个部分提交后使用jQuery,因此无法正常工作,因为有两个部分每个部分都有一个提交按钮,第一个部分工作正常,但我同时尝试验证数据并将数据提交给服务器第二阶段,它似乎在按照我的代码结构方式执行验证检查之前提交数据?在您的问题中向我们显示HTML。不认为这会起作用吗?我不想阻止表单提交,只是在首先检查验证之后发生!如果我要将所有代码移到一个函数d中o你认为这会有助于解决问题吗?是的。如果验证失败,你可以调用preventDefault,否则表单将正确提交。我最终解决了问题。干杯!
$('#submit_second').submit(function(evt){
    evt.preventDefault();
    ...
});