你能从js获得asp:textbox输入的pattern属性来验证表单吗?

时间:2016-11-16 17:17:07

标签: jquery asp.net

这是我的js验证功能。如何从js获取asp:textbox输入的pattern属性以验证表单?

<form id="obrasform">
 <asp:TextBox ID="txtbox" pattern="[\d]{3}" runat="server"/>
</form>

 <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js"></script>
        <script>
            $(document).ready(function () {
                $('#obrasform').validate({
                    ignore: [],   
                    invalidHandler: function (event, validator) {
                        var id = jQuery(validator.errorList[0].element).closest(".tab-pane").attr('id');             
                        $('.nav-tabs a[href="#' + id + '"]').tab('show');
                    },         
   }); 
        });
    </script>

1 个答案:

答案 0 :(得分:0)

更好地查看asp.net Validation Controls。它比为每个TextBox创建一个函数容易得多。

<asp:TextBox ID="TextBox1" runat="server" ValidationGroup="myGroup"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="myGroup" />
<br />
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1" ValidateEmptyText="true" ErrorMessage="Value is invalid" ValidationGroup="myGroup" ClientValidationFunction="myCustomValidation"></asp:CustomValidator>

<script type="text/javascript">
    function myCustomValidation(oSrc, args) {
        var filter = /[\d]{3}/;
        args.IsValid = filter.test(args.Value);
    }
</script>