Jquery 表单验证无法正常工作

阿鲁·马尼卡姆

我的代码问题是验证工作不正常,即;如果我们输入名字,它会显示错误消息“名称应该是 3 个字符”,但它没有禁用提交按钮,因为所有字段都显示错误消息,但它已成功提交。所以帮帮我吧只有在所有验证都正确时才提交。我正在正确编写代码,但我不明白问题出在哪里。

   <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
        <style>
            @import url('https://fonts.googleapis.com/css?family=Signika');
    
            .nopadding {
                padding: 0px;
            }
    
            .align {
                margin-left: 50%;
            }
    
            .inputtop {
                font-family: 'Signika', sans-serif;
                margin-top: 10px;
            }
    
            .signup {
                font-family: 'Signika', sans-serif;
                text-align: center;
                color: #31d3fb;
                font-size: 50px;
                height: 50px;``
            }
    
            .set {
                padding-right: 0px;
                padding-left: 0px;
            }
    
            .copyright {
                margin-top: 33px;
            }
    
            .panel-primary {
                border-color: #fff;
            }
    
            .panel-primary>.panel-heading {
                background: #bce8f1;
            }
    
            .panel-primary>.panel-body {
                background-color: #fff;
            }
        </style>
    
    
    </head>
    
    
    <div>
        <div class="col-md-7 nopadding color">
            <img src="images/energywallpaper2.jpg" width="100%" height="920px" alt="">
        </div>
        <div class="col-md-5 color">
    
            <div class="panel panel-primary">
                <h1 class="signup">Signup</h1>
                <div class="panel-body">
                    <form name="myform" method="post">
                        <div class="form-group">
                            <label for="fname">First Name *</label>
                            <input id="fname" name="fname" class="form-control" type="text" data-validation="required">
                            <span id="error_name" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label for="lname">Last Name *</label>
                            <input id="lname" name="lname" class="form-control" type="text" data-validation="email">
                            <span id="error_lname" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label for="email">Email *</label>
                            <input type="text" id="email" name="email" class="form-control">
                            <span id="error_email" class="text-danger"></span>
                        </div>
    
                        <div class="form-group">
                            <label for="password">Password *</label>
                            <input type="password" id="password" name="password" class="form-control">
                            <span id="error_password" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label for="password">Confirm password *</label>
                            <input type="password" id="cpassword" name="cpassword" class="form-control">
                            <span id="error_cpassword" class="text-danger"></span>
                        </div>
                        <div class="form-group">
                            <label for="disc">Private Notes</label>
                            <textarea class="form-control" rows="3" col="50"></textarea>
                        </div>
                        <div class="form-group">
                            <label for="disc">Visible Notes</label>
                            <textarea class="form-control" rows="3" col="50"></textarea>
                        </div>
                        <div class="form-group">
                            <label for="dob">Date Of Birth *</label>
                            <input type="text" name="dob" id="dob" class="form-control">
                            <span id="error_dob" class="text-danger"></span>
                        </div>
                        <button id="submit" type="submit" value="submit" class="btn btn-primary center">Submit</button>
                        <div class="clearfix"></div>
    
                        <div class="separator">
                            <p class="change_link">Already a member ?
                                <a href="#signin" class="to_register"> Log in </a>
                            </p>
    
                            <div class="clearfix"></div>
                            <br />
    
                            <div>
                                <p class="copyright">AMK website©2017 All Rights Reserved.</p>
                            </div>
                        </div>
    
                </div>
                </form>
    
            </div>
        </div>
    </div>
    
    </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
    <script>
        $("#dob").datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: "dd/mm/yy",
            yearRange: "-90:+00"
        });
    </script>
    <script type="text/javascript">
        $(document).ready(function() {
            $flag = 1;
            $("#fname").focusout(function() {
    
                if ($(this).val() == '') {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_name").text("* You have to enter your first name!");
                } else if ($(this).val().length < 3) {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_name").text("*You have to enter minimum 3 characters of your first name!");
                } else {
                    $(this).css("border-color", "#2eb82e");
                    $('#submit').attr('disabled', false);
                    $("#error_name").text("");
    
                }
            });
            $("#lname").focusout(function() {
                if ($(this).val() == '') {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_lname").text("* You have to enter your Last name!");
                } else if ($(this).val().length < 3) {
    
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_lname").text("*You have to enter minimum 3 characters of your last name!");
                } else {
                    $(this).css("border-color", "#2eb82e");
                    $('#submit').attr('disabled', false);
                    $("#error_lname").text("");
                }
            });
            $("#dob").focusout(function() {
                if ($(this).val() == 'null') {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_dob").text("* You have to enter your Date of Birth!");
                } else {
                    $(this).css("border-color", "#2eb82e");
                    $('#submit').attr('disabled', false);
                    $("#error_dob").text("");
                }
            });
    
            $("#email").focusout(function() {
                var email = $("#email").val();
                var pattern = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
    
                if ($(this).val() == "") {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_email").text("* You have to enter your email!");
                } else if (!pattern.test(email)) {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_email").text("* Please enter valid email");
                } else {
                    console.log("success");
                    $(this).css({
                        "border-color": "#2eb82e"
                    });
                    $('#submit').attr('disabled', false);
                    $("#error_email").text("");
    
                }
            });
            $("#password").focusout(function() {
                var pass = $("#password").val();
                var strength = 0;
                //if password contains both lower and uppercase characters, increase strength value
                if (pass.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1
    
                //if it has numbers and characters, increase strength value
                if (pass.match(/([a-zA-Z])/) && pass.match(/([0-9])/)) strength += 1
    
                //if it has one special character, increase strength value
                if (pass.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
    
                //if it has two special characters, increase strength value
                if (pass.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 1
    
                if (pass == '') {
                    //console.log("test" + strength)
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_password").text("* You have to enter your password !");
                } else if ($("#password").val().length < 8) {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_password").text("*You have to enter minimum 8 characters of your password !");
                } else if (strength < 1) {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_password").text("*your password is weak!please use letters,special symbols and numbers");
                } else {
                    $(this).css({
                        "border-color": "#2eb82e"
                    });
                    $('#submit').attr('disabled', false);
                    $("#error_password").text("");
                }
            });
            $("#cpassword").focusout(function() {
                cpass = $("#cpassword").val();
                if (cpass == '') {
                    $(this).css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_cpassword").text("* You have to re-enter your password !");
                } else {
                    $(this).css({
                        "border-color": "#2eb82e"
                    });
                    $('#submit').attr('disabled', false);
                    $("#error_cpassword").text("");
                }
    
            });
    
            $("#submit").click(function() {
                if ($("#fname").val() == '') {
                    $("#fname").css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_name").text("* You have to enter your first name!");
                }
                if ($("#lname").val() == '') {
                    $("#lname").css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_lname").text("* You have to enter your Last name!");
                }
                if ($("#dob").val() == '') {
                    $("#dob").css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_dob").text("* You have to enter your Date of Birth!");
                }
                if ($("#email").val() == '') {
                    $("#email").css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_email").text("* You have to enter your email!");
                }
                if ($("#password").val() == '') {
                    $("#password").css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_password").text("* You have to enter your password!");
                }
                if ($("#cpassword").val() == '') {
                    $("#cpassword").css("border-color", "#FF0000");
                    $('#submit').attr('disabled', true);
                    $("#error_cpassword").text("* You have to re-enter your password!");
                }
            });
    
        });
    </script>

ItDuckyyyy

如果验证失败,则需要在提交按钮的单击函数结束时返回 false。这将阻止表单提交。

$("#submit").click(function() {
        var passedValidation = true;
        if ($("#fname").val() == '') {
            $("#fname").css("border-color", "#FF0000");
            $('#submit').attr('disabled', true);
            $("#error_name").text("* You have to enter your first name!");
            passedValidation = false;
        }
        if ($("#lname").val() == '') {
            $("#lname").css("border-color", "#FF0000");
            $('#submit').attr('disabled', true);
            $("#error_lname").text("* You have to enter your Last name!");
            passedValidation = false;
        }
        if ($("#dob").val() == '') {
            $("#dob").css("border-color", "#FF0000");
            $('#submit').attr('disabled', true);
            $("#error_dob").text("* You have to enter your Date of Birth!");
            passedValidation = false;
        }
        if ($("#email").val() == '') {
            $("#email").css("border-color", "#FF0000");
            $('#submit').attr('disabled', true);
            $("#error_email").text("* You have to enter your email!");
            passedValidation = false;
        }
        if ($("#password").val() == '') {
            $("#password").css("border-color", "#FF0000");
            $('#submit').attr('disabled', true);
            $("#error_password").text("* You have to enter your password!");
            passedValidation = false;
        }
        if ($("#cpassword").val() == '') {
            $("#cpassword").css("border-color", "#FF0000");
            $('#submit').attr('disabled', true);
            $("#error_cpassword").text("* You have to re-enter your password!");
            passedValidation = false;
        }

        return passedValidation;
    });

此外,在您的密码验证检查中,您不确认两个密码匹配。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

表单上的jQuery验证无法正常工作

来自分类Dev

jQuery表单验证无法正常工作

来自分类Dev

jQuery表单验证无法正常工作

来自分类Dev

jQuery 验证无法正常工作

来自分类Dev

使用jQuery的表单无法正常工作

来自分类Dev

我正在尝试使用jquery插件来验证我的表单,但删除类却无法正常工作

来自分类Dev

验证无法使用jQuery请求正常工作

来自分类Dev

jQuery替换INPUT对象后,PHP表单无法正常工作

来自分类Dev

尝试从表单获取值时,jQuery Val无法正常工作

来自分类Dev

电子邮件验证Jquery无法正常工作

来自分类Dev

使用ajax进行jQuery验证无法正常工作

来自分类Dev

使用引导程序的jquery验证错误消息无法正常工作

来自分类Dev

删除上传的图片后,jQuery验证无法正常工作

来自分类Dev

追加新元素后,jQuery 验证无法正常工作

来自分类Dev

jQuery .on()无法正常工作

来自分类Dev

jQuery .on()无法正常工作

来自分类Dev

jQuery $('a')无法正常工作

来自分类Dev

jQuery无法正常工作。jQuery的

来自分类Dev

ASP MVC jQuery验证onfocusout正常工作,但不提交表单

来自分类Dev

由于存在JQuery冲突而导致没有页面刷新的JQuery表单提交无法正常工作

来自分类Dev

bpopup jQuery无法正常工作

来自分类Dev

jQuery冲突无法正常工作

来自分类Dev

jQuery Valildator无法正常工作

来自分类Dev

.css()jQuery无法正常工作

来自分类Dev

jQuery tinysort无法正常工作

来自分类Dev

jQuery Fancybox无法正常工作

来自分类Dev

jQuery Datepicker无法正常工作

来自分类Dev

jQuery .slideUp()无法正常工作

来自分类Dev

jQuery .next()无法正常工作