weixin_33737134 2013-05-22 14:35 采纳率: 0%
浏览 24

Ajax表单忽略验证

I have not used ajax much before, so opted for the jQuery Form plugin. My aim is to have the form submitted by the plugin, use my validation to check the form fields are filled out correctly, if so submit the form.

All of the above works, except validation. When I click 'submit' the validation shows briefly then the box fades out as I have set in the success call in the ajax form script.

var options = { 
    beforeSubmit: validation,
    success: function() {
        var url = '/includes/embed-topbar-login-account';
        $('#account').load(url, function() {
            $('#account-options').fadeOut('slow');
        });
    }
}; 

$('#login').ajaxForm(options); 

function validation() {
    var prompt = '<div class="required-prompt"><small>This field is required.</small></div>';
    var promptEmail = '<div class="required-prompt"><small>Invalid email address.</small></div>';
    var required = true;

        $('.required-prompt', this).remove();
        $('.required', this).each(function() {
            if( !$(this).val() ) {
                $(this).after(prompt);
                required = false;
            } else {
                $('.required-prompt', this).remove();
            }
        });

    return required;
}

Can anyone tell me if there are any extra steps required to ensure the submission is halted if the validation returns 'false' (which it currently does).

  • 写回答

1条回答 默认 最新

  • weixin_33705053 2013-05-22 14:58
    关注

    you shsould specify beforeSubmit

    SOURCE Documentation

    $('#login').ajaxForm({ 
        success: function() {
            var url = '/includes/embed-topbar-login-account';
            $('#account').load(url, function() {
                $('#account-options').fadeOut('slow');
            });
        }, 
        beforeSubmit: function(arr, $form, options) { 
        // The array of form data takes the following form: 
        // [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ] 
    
        // return false to cancel submit                  
         return validate(); //Assuming validate is your function doing validation.
    
        }
    }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮