Bootstrap验证日期dd.mm.yyyy

时间:2016-05-29 15:33:01

标签: regex twitter-bootstrap validation date

我有bootstrapdatepicker,当我点击选择日期时,我的验证会保持沉默。

我只是希望我的现场日期完美,因为它适用于dd / mm / yyyy格式。

我是否需要创建回调函数,或者我只需要合并一个正则表达式:

/((?:0[1-9]|[12]\d|3[01])(?=\.(?:0[13578]|1[02]))|(?:0[1-9]|[12]\d|30)(?=\.(?:0[469]|11))|(?:0[1-9]|1\d|2[0-8]|29(?=\.\d{2}\.\d*(?:(?:(?!\d{2}00)(?=\d{2}(?:[13579][26]|[02468][048])))|(?=(?:[13579][26]|[02468][048])00))\d{4}(?!\d)))(?=\.02))\.(\d{2})\.(\d{4,})/,

我对日期的验证如下:

<script> var validator = $("#modalCreateMerge").bootstrapValidator({
        feedbackIcons: {
            valid: "glyphicon glyphicon-ok",
            invalid: "glyphicon glyphicon-remove",
            validating: "glyphicon glyphicon-refresh"
        },
        fields: {
            Date: {
                validators: {
                    notEmpty: {
                        message: "Please enter a date"
                    },
                    regexp: {
                        regexp: /((?:0[1-9]|[12]\d|3[01])(?=\.(?:0[13578]|1[02]))|(?:0[1-9]|[12]\d|30)(?=\.(?:0[469]|11))|(?:0[1-9]|1\d|2[0-8]|29(?=\.\d{2}\.\d*(?:(?:(?!\d{2}00)(?=\d{2}(?:[13579][26]|[02468][048])))|(?=(?:[13579][26]|[02468][048])00))\d{4}(?!\d)))(?=\.02))\.(\d{2})\.(\d{4,})/,
                        message: 'Please provide a concrete date (dd.mm.yyyy)'
                    },
                    date: {
                        format: 'MM/DD/YYYY',
                        message: 'The date is not a valid'
                    }
                }
            },
            Client1: {
                validators: {
                    notEmpty: {
                        message: "Please enter a client"
                    }
                }
            },
            FCOID1: {
                validators: {
                    notEmpty: {
                        message: "Please enter a type"
                    }
                }
            },
            valueMoney: {
                validators: {
                    notEmpty: {
                        message: "Please enter money value"
                    }
                }
            },
            valueHR: {
                validators: {
                    notEmpty: {
                        message: "Please enter HR value"
                    }
                }
            },
            Status10: {
                validators: {
                    notEmpty: {
                        message: "Please ente a status"
                    }
                }
            },
            commentarea: {
                validators: {
                    notEmpty: {
                        message: "Please provide a comment"
                    }
                }
            }
        }
    });
    validator.on("success.form.bv", function (e) {
        e.preventDefault();
        $('#modalCreateMerge').data('bootstrapValidator').resetForm();
        var $form = $(e.target);
        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');
        // Use Ajax to submit form data
        $.post($form.attr('action'), $form.serialize(), function (result) {
            console.log(result);
        }, 'json');
  });
 </script>

我的表单如下:

  <div class="modal fade" id="modalCreateMerge" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">
                <span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Add pipeline!</h4>
        </div>
        <div class="modal-body">
            <div id="formregister">
                <form action="" class="form-horizontal" role="form" id="modalCreateMerge">
                    <p class="qc-errmsg" style="display: none;">&nbsp;</p>
                    <div class="form-group">
                        <label for="Date" class="col-sm-2 control-label">Date</label>
                        @*<div class="col-sm-10">
                            <input type="text" class="form-control" id="datepickerCreateModal1" name="Date" placeholder="dd.mm.yyyy">
                        </div>*@
                        <div class="input-group input-append date" id="datepickerCreateModal1">
                            <input type="text" class="form-control" name="Date" />
                            <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="Client" class="col-sm-2 control-label">Client</label>
                        <div class="col-sm-10">
                            @Html.DropDownList("Client1", (SelectList)ViewBag.Client, "--Choose a client--", new { @class = "form-control", tabindex = "1" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="Field of Co-Op" class="col-sm-2 control-label">Field of Co-Op</label>
                        <div class="col-sm-10">
                            @Html.DropDownList("FCOID1", (SelectList)ViewBag.FCOID, "--Choose a type of FCO--", new { @class = "form-control", tabindex = "2" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="valuemoney" class="col-sm-2 control-label">Value Money</label>
                        <div class="col-sm-10">
                            <input type="text" tabindex="3" class="form-control" tabindex="3" id="valueMoney" name="valueMoney" placeholder="Value of the pipeline?">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="valueHR" class="col-sm-2 control-label">Value HR</label>
                        <div class="col-sm-10">
                            <input type="text" tabindex="4" class="form-control" id="valueHR" name="valueHR" placeholder="How many people?">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="Status" class="col-sm-2 control-label">Status</label>
                        <div class="col-sm-10">
                            @Html.DropDownList("Status10", (SelectList)ViewBag.StatusID, "--Choose Status--", new { @class = "form-control", tabindex = "5" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="Comment" class="col-sm-2 control-label">Comment</label>
                        <div class="col-sm-10">
                            <textarea rows="4" cols="50" tabindex="6" class="form-control" name="commentarea" id="commentModal" placeholder="Enter a comment"></textarea>
                            @*<input type="text" tabindex="6" class="form-control" id="commentModal" name="commentModal" placeholder="Enter a comment">*@
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" tabindex="7" value="cart" class="btn btn-primary">Save Changes</button>
                            <button type="button" tabindex="8" class="close1 btn btn-default" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </form>
            </div>
            <!-- form register -->
            <div id="successfulpost" style="font: bold 12px Verdana, Arial, Helvetica, sans-serif; color: #ff0000; display: none;">
                <p class="jst-txt">
                    <span>Thank you,</span> for showing your Interest !!
                </p>
                <p class="jst-txt">Our property advisor shall get in touch with you very shortly..</p>
            </div>
        </div>
        <!-- model body-->
    </div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

如果可能的话,可能会稍微简化一下。     (0 [1-9] | [12] [0-9] | 3 [01]) - /.- /。[0-9] {2}