你如何验证jquery / ajax表单?

时间:2016-07-10 23:21:21

标签: javascript jquery ajax

所以我有这个注册表单,我如何验证这个,因为每次我提交和eventHough没有数据,每次按下按钮时它都会被添加到数据库中。到目前为止,这是我的代码:

<form id="signupform" class="form" method="post" action="#">
     <div class="input-prepend" ><span class="add-on"><i class="glyphicon glyphicon-user"></i></span>
       <label for="name">Your Full Name</label>
            <input type="text" id="name" name="name" placeholder="your full name" size="40">
     </div>

     <br />

     <div class="input-prepend"><span class="add-on"><i class="glyphicon glyphicon-envelope"></i></span>
            <label for="email">Your Email</label>
                <input type="email" id="email" name="email" placeholder="your@email.com" size="40" style="color:black;" class="validate" required>
     </div>

     <br />

     <button type="submit" class="btn btn-large" name="action"> Sign Up!</button>
</form>

这就是我的jquery代码:

 $(document).ready(function(){
        jQuery("#signupform").on("submit",function(e){
            e.preventDefault();
            var name = jQuery("#name").val();
            var email = jQuery("#email").val();
            var fd = new FormData();
            fd.append("name",name);
            fd.append("email",email);
            jQuery.ajax({
                type: 'POST',
                url: 'signup.php',
                data: fd,
                processData: false,
                contentType: false
            })
            jQuery('#signupform')[0].reset();    

        });


    });

1 个答案:

答案 0 :(得分:0)

$(document).ready(function(){
        jQuery("#signupform").on("submit",function(e){
            e.preventDefault();
            var name = jQuery("#name").val();
            var email = jQuery("#email").val();

            if (!nome || !email || !email.match(/.+@.+[.].+/))
            {
                alert("complete the form before submit");
                return;
            }

            var fd = new FormData();
            fd.append("name",name);
            fd.append("email",email);
            jQuery.ajax({
                type: 'POST',
                url: 'signup.php',
                data: fd,
                processData: false,
                contentType: false
            })
            jQuery('#signupform')[0].reset();    

        });


    });