我需要有关js和jquery表单验证的帮助

时间:2013-10-14 16:09:59

标签: javascript jquery ajax validation

我有这个问题的问题,当我填写我的电子邮箱正确的电子邮件,它可以验证,然后问我正确的电子邮件,当我更改我的电子邮箱为空时,它显示我正确的电子邮件地址。让我们检查http://www.icodecrew.com/register

             if(email == "") {
        $("span.val_email").html("Please Enter Your Correct Email-ID.").addClass('validate');
        validation_holder = 1;
    } else { 
        if(!email_regex.test(email)){ // if invalid email
            $("span.val_email").html("Invalid Email!").addClass('validate');
            validation_holder = 1;
        } else {
  $("span.val_email").html("");

                 $(document).ready(function(){
        $("#email").change(function(){

             $("span.val_email").html("<img src='spinner.gif' /> Please wait until we check...");


        var email=$("#email").val();

          $.ajax({
                type:"POST",
                url:"includes/checkemail.php",
                data:"email="+email,
                    success:function(data){
                    if(data==0){
                        $("span.val_email").html("<img src='accept.png' title='available' />");
                    }
                    else{
                        $("span.val_email").html("<img src='error.png' /> E-Mail is Already registered");
                    }
                }
             });

        });

     }); 
        }
    }

1 个答案:

答案 0 :(得分:0)

正如@PlantTheldea在您的问题评论中提到的那样,您的操作顺序严重受损。通读了您的代码,这是我能够达到我认为您想要的最接近的代码。它是 UNTESTED ,而仅用于帮助您确定应执行步骤的顺序

$(function () {
    var email_input = $('#email');

    email_input.change(function () {
        var status_display = $('span.val_email'),
            email = email_input.val();

        if (email === '') {
            status_display
                .html('Please Enter Your Correct Email-ID.')
                .addClass('validate');

            validation_holder = 1;
        } else { 
            if (!email_regex.test(email)) {
                status_display
                    .html('Invalid Email!')
                    .addClass('validate');

                validation_holder = 1;
            } else {
                status_display
                    .html('<img src="spinner.gif" /> Please wait until we check...')
                    .removeClass('validate');

                $.ajax({
                    type: 'POST',
                    url: 'includes/checkemail.php',
                    data: {
                        email: email
                    },
                    success: function (data) {
                        if (data == 0){
                            status_display.html('<img src="accept.png" title="available" />');
                        } else {
                            status_display.html('<img src="error.png" /> E-Mail is Already registered');
                        }
                    }
                });
            }
        }
    });
});