开发者

Validate phone number by custom validator and javascript

开发者 https://www.devze.com 2023-03-16 12:50 出处:网络
I have a phoneTextBox control, which contains 4 TextBoxes: country code (1-3 digits), city code (1-7 digits),

I have a phoneTextBox control, which contains 4 TextBoxes:

country code (1-3 digits), city code (1-7 digits), local number (1-7 digits) and extra phone number (1-5 digits).

The extra phone number is not required.

The code below doesn't work.

    <script type="text/javascript">
    function ValidatePhoneNumber(source, args) 

    {
        if (     $('#<%=txtCountryCode.ClientID %>').val().match(/^\d{1,3}$) ||
                 $('#<%=txtCityCode.ClientID %>').val().match(/^\d{1,7}$) ||
                 $('#<%=txtMainPhoneNumber.ClientID %>').val().match(/^\d{1,7}$)
           )


        {
            if ($('#<%=txtExtraPhoneNumber.ClientID %>').val().length<=0)
            {
               args.IsValid = true;
               return;
            }
            else 
            {
                if ($('#<%=txtExtraPhoneNumber.ClientID %>').val().match(/^\d{1,5}$)
                {
                    args.IsValid = true;
           开发者_开发技巧        return;

                }
                else 
                {
                    args.IsValid = false;

                }

            }
        }
        else 
                {
                    args.IsValid = false;

                }

}
</script>
    <div style="display: inline">
        <asp:CustomValidator runat="server" ForeColor="Red" ErrorMessage="Invalid format" ClientValidationFunction="ValidatePhoneNumber" />
        <div>
            <b>+</b><asp:TextBox ID="txtCountryCode" runat="server" Width="30px" MaxLength="3"></asp:TextBox>
            <asp:TextBox ID="txtCityCode" runat="server" Width="60px" MaxLength="7"></asp:TextBox>
            <asp:TextBox ID="txtMainPhoneNumber" runat="server" Width="60px" MaxLength="7"></asp:TextBox>
            <asp:TextBox ID="txtExtraPhoneNumber" runat="server" Width="50px" MaxLength="5"></asp:TextBox>
        </div>
    </div>


    args.IsValid = $('#<%=txtCountryCode.ClientID %>').val().match(/^\d{1,3}$/) &&
                 $('#<%=txtCityCode.ClientID %>').val().match(/^\d{1,7}$/) &&
                 $('#<%=txtMainPhoneNumber.ClientID %>').val().match(/^\d{1,7}$/) &&
$('#<%=txtExtraPhoneNumber.ClientID %>').val().match(/^\d{0,5}$/);


you are missing to end all the regex with /

0

精彩评论

暂无评论...
验证码 换一张
取 消