dragonfly9527
dragonfly9527
2014-08-04 12:55
采纳率: 100%
浏览 58

JavaScript表单验证问题 - 必须单击提交两次

JavaScript code of validation- https://freedigitalphotos.net/images/js/judd-validate.js

We have some new forms on our website which have client side JavaScript validation.

The validation is triggered when the user "defocuses" from the form fields. The validation is a combination of AJAX (checking database for valid user names etc) and JavaScript (checking fields not blank, or contain expected data).

The user has to click the form submit button twice to send the form. It seems that the first click is triggering the field validation but not submitting the form. Clicking for a second time successfully completes the form.

Go to- https://freedigitalphotos.net/images/recover_password.php Type arifkpi@gmail.com in the email field, and then immediately hit submit. Again, notice that the first click merely validates the input, using AJAX it is checking the email address is in the database. A second click is required.

I want to fix this, so everything will work with a single click.

图片转代码服务由CSDN问答提供 功能建议

验证的JavaScript代码 - https://freedigitalphotos.net/images/js/judd-validate.js

我们的网站上有一些新表格 它具有客户端JavaScript验证。

当用户从表单字段“defocuses”时,将触发验证。 验证是AJAX(检查有效用户名的数据库等)和JavaScript(检查字段不是空白,或包含预期数据)的组合。

用户必须单击表单提交按钮 两次发送表格。 似乎第一次点击是触发字段验证但不提交表单。 单击第二次成功完成表单。

转到 - https ://freedigitalphotos.net/images/recover_password.php 在电子邮件字段中输入arifkpi@gmail.com,然后立即点击提交。 再次注意,第一次单击只是验证输入,使用AJAX检查电子邮件地址是否在数据库中。 需要再次点击。

我想解决这个问题,所以只需点击一下即可完成所有操作。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumou5109
    doumou5109 2014-08-04 13:23
    已采纳

    Instead of calling Ajax validations on focustout event, you can call it on click of your button and if Ajax returns true result then submit form programatically. Refer few sample code lines:

    Html Part:

    <form method="post" id="registration_form" name="registration_form" action="register.php">
      EmailId: <input type="text" id="email_id" name="email_id">
      <label class="error" for="username" id="globalErrorUsername"></label>
      Username: <input type="text" id="username" name="username">
      <label class="error" id="globalErrorEmail"></label>
      <button type="button" id="register_btn" name="register_btn">Register</button>
    </form>
    

    Js Part:

    $("#register_btn").click(function() {
        //.valid function is useful when you are using jquery Validate library for other field validation
        if ($('#registration_form').valid()) {
            var email_id = $('#registration_form').find('#email_id').val(),
                username = $('#registration_form').find('#username').val();
            $.ajax({
                beforeSend: function() {
                    $('#globalErrorUsername').html('');
                    $('#globalErrorEmail').html('');
                 }, //Show spinner
                complete: function() {  },
                type : 'post',
                url : 'check-user-existence.php',
                dataType :'json',
                data : 'email_id=' + email_id + '&username=' + username,
                success:function(response) {
                    if(response.success == 1) {
                        //submit the form here
                        $("#registration_form").submit();
                    } else if (response.error == 1) {
                        if(response.details.username != undefined) {
                            $('#globalErrorUsername').show();
                            $('#globalErrorUsername').html(response.details.username);
                        }
                        if(response.details.email_id != undefined) {
                            $('#globalErrorEmail').show();
                            $('#globalErrorEmail').html(response.details.email_id);
                            $('#email_id').focus();
                        } else {
                            $('#username').focus();
                        }
    
                    }
                }
            });
        } else {
            return false;
        }
        return false;
    });
    
    点赞 评论

相关推荐