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

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.

  • 写回答

1条回答 默认 最新

  • 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;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?