【问题标题】:JQuery Validate individual fieldsetsJQuery 验证单个字段集
【发布时间】:2011-04-10 06:58:19
【问题描述】:

我开发了一个包含多个字段集的表单来表示填写完整表单的步骤。通过按钮单击显示和隐藏字段集(每个字段集上一个),但我想在显示下一个字段集之前验证每个字段集。

我是 JQuery 新手,遇到了一些麻烦。我发现本指南 (http://encosia.com/2009/11/24/asp-net-webforms-validation-groups-with-jquery-validation/) 允许我独立验证不同的字段集,但我的问题是如何使用该验证来控制相关字段集的显示和隐藏。

我认为这样做的方法是从按钮的每个单击事件中创建一个函数,但我似乎无法正确调用验证函数。

恐怕我现在完全糊涂了!救命!!

【问题讨论】:

    标签: javascript jquery validation


    【解决方案1】:

    您可以为每个字段集编写自定义验证函数并使用 .keyup 函数调用它。这是一个例子:

    HTML:

    <div id="fieldset1">
        <input type="text" name="fname" id="fname">
    </div>
    
    <div id="fieldset2">
        <!--hidden using css-->
        <input type="text" name="fname" id="fname">
    </div>
    

    Javascript:

    $('#fieldset1 #fname').keyup(function () {
        // Runs every time your keystroke is released on this input
        if($(this).val() == 'Adam') {
            // Checks to see if the field's value is Adam. If it is, then it shows the next fieldset. Otherwise it hides it.
            $('#fieldset2').show();
        } else {
            $('#fieldset2').hide();
        }
    }
    

    这显然是一个非常简单的示例,但它说明了您需要做什么才能验证表单并根据用户输入修改 DOM。

    【讨论】:

    • 感谢您的回复,我在您的回答中看到了逻辑,但不想在将数据输入字段时自动显示下一个字段集。我可以将字段集的验证绑定到按钮吗?例如创建一个函数 validateFS1(),如果字段集有效,则返回 true。
    • 是的 - 您可以使用按钮或链接 - 只需插入一些代码 onClick="validateFS1()" 并在验证返回 true 时使用该函数显示下一个字段集。
    【解决方案2】:

    使用jQuery validation 插件,您可以通过在同一页面中使用多个表单来解决此问题。然后你可以调用:

    $('#myFormId').validate()

    关于当时感兴趣的一种形式。

    【讨论】:

    • 很高兴知道,但我认为将所有字段都放在一个表单中是有好处的,可能更容易提交
    【解决方案3】:

    我在谷歌搜索其他内容时发现你的问题之前偶然发现了这个例子,希望这能帮助遇到同样问题的人:

    https://djaodjin.com/blog/jquery-multi-step-validation.blog.html

    基本上,validation.js 默认只验证可见字段。

    这是完整的示例代码,点击链接查看解释:

    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <title>Multiple step form</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
        <script type="text/javascript" src="js/jquery-1.9.0.js"></script>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>
        <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/additional-methods.js"></script>
        <style type="text/css">
            #personal_information,
            #company_information{
                display:none;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="col-lg-5">
                <form class="form-horizontal" action="" method="POST" id="myform">
    
                    <fieldset id="account_information" class="">
                        <legend>Account information</legend>
                        <div class="form-group">
                            <label for="username" class="col-lg-4 control-label">Username</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" id="username" name="username" placeholder="username">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="password" class="col-lg-4 control-label">Password</label>
                            <div class="col-lg-8">
                                <input type="password" class="form-control" id="password" name="password" placeholder="Password">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="conf_password" class="col-lg-4 control-label">Confirm password</label>
                            <div class="col-lg-8">
                                <input type="password" class="form-control" id="conf_password" name="conf_password" placeholder="Password">
                            </div>
                        </div>
                        <p><a class="btn btn-primary next">next</a></p>
                    </fieldset>
    
                    <fieldset id="company_information" class="">
                        <legend>Account information</legend>
                        <div class="form-group">
                            <label for="company" class="col-lg-4 control-label">Company</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" id="company" name="company" placeholder="Company">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="url" class="col-lg-4 control-label">Website url</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" id="url" name="url" placeholder="Website url">
                            </div>
                        </div>
                        <p><a class="btn btn-primary next">next</a></p>
                    </fieldset>
    
                    <fieldset id="personal_information" class="">
                        <legend>Personal information</legend>
                        <div class="form-group">
                            <label for="name" class="col-lg-4 control-label">Name</label>
                            <div class="col-lg-8">
                                <input type="text" class="form-control" id="name" name="name" placeholder="Name">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="email" class="col-lg-4 control-label">Email</label>
                            <div class="col-lg-8">
                                <input type="email" class="form-control" id="email" name="email" placeholder="Email">
                            </div>
                        </div>
                        <p><a class="btn btn-primary" id="previous" >Previous</a></p>
                        <p><input class="btn btn-success" type="submit" value="submit"></p>
                    </fieldset>
    
                </form>
            </div>  
        </div>
    
        <script type="text/javascript">
            $(document).ready(function(){
    
                // Custom method to validate username
                $.validator.addMethod("usernameRegex", function(value, element) {
                    return this.optional(element) || /^[a-zA-Z0-9]*$/i.test(value);
                }, "Username must contain only letters, numbers");
    
                $(".next").click(function(){
                    var form = $("#myform");
                    form.validate({
                        errorElement: 'span',
                        errorClass: 'help-block',
                        highlight: function(element, errorClass, validClass) {
                            $(element).closest('.form-group').addClass("has-error");
                        },
                        unhighlight: function(element, errorClass, validClass) {
                            $(element).closest('.form-group').removeClass("has-error");
                        },
                        rules: {
                            username: {
                                required: true,
                                usernameRegex: true,
                                minlength: 6,
                            },
                            password : {
                                required: true,
                            },
                            conf_password : {
                                required: true,
                                equalTo: '#password',
                            },
                            company:{
                                required: true,
                            },
                            url:{
                                required: true,
                            },
                            name: {
                                required: true,
                                minlength: 3,
                            },
                            email: {
                                required: true,
                                minlength: 3,
                            },
    
                        },
                        messages: {
                            username: {
                                required: "Username required",
                            },
                            password : {
                                required: "Password required",
                            },
                            conf_password : {
                                required: "Password required",
                                equalTo: "Password don't match",
                            },
                            name: {
                                required: "Name required",
                            },
                            email: {
                                required: "Email required",
                            },
                        }
                    });
                    if (form.valid() === true){
                        if ($('#account_information').is(":visible")){
                            current_fs = $('#account_information');
                            next_fs = $('#company_information');
                        }else if($('#company_information').is(":visible")){
                            current_fs = $('#company_information');
                            next_fs = $('#personal_information');
                        }
    
                        next_fs.show(); 
                        current_fs.hide();
                    }
                });
    
                $('#previous').click(function(){
                    if($('#company_information').is(":visible")){
                        current_fs = $('#company_information');
                        next_fs = $('#account_information');
                    }else if ($('#personal_information').is(":visible")){
                        current_fs = $('#personal_information');
                        next_fs = $('#company_information');
                    }
                    next_fs.show(); 
                    current_fs.hide();
                });
    
            });
        </script>
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 2011-02-13
      • 2018-09-08
      相关资源
      最近更新 更多