How do I validate a asp.net field using javascript and pattern matching

TechIsaac
TechIsaac used Ask the Experts™
on
I am trying to validate the presence and pattern of a field defined in an asp. net page.
The presence of the field works but when I enter the telephone number in the
field for pattern matching, it does not work. I get the error message.

Please advise,
Isaac

<div visible="true" id="divTelephoneNumber" runat="server">
                  <div class="col-xs-12 col-md-8 col-sm-10 col-lg-6 nill">
					<asp:Label ID="lblTelephoneNumber" runat="server" AssociatedControlID="txtTelephoneNumber" Text="Telephone number *" CssClass="control-label" />
                      <br />
					<asp:TextBox ID="txtTelephoneNumber" runat="server" CssClass="form-control" MaxLength="20" aria-label="Telephone number" ></asp:TextBox>
					<span class="required"></span>
                    <asp:CustomValidator id="rfVTN" runat="server" ControlToValidate="txtTelephoneNumber"  ValidateEmptyText="true" ErrorMessage="Telephone Number is a required field. nnn-nnn-nnnn " ClientValidationFunction="ValidateTelephone"/> 
                  </div>
                </div>            



 <script type="text/javascript">
       function ValidateTelephone(sender, args) {
           var compare = RegExp("/^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/");
           args.IsValid = compare.test(args.Value);
           return;
       }
</script>        

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior .Net Full Stack Developer
Commented:
since there might be different phone formats you are doing...here is a link that shows you patterns and you should be able to choose the format that fits your validation.

http://regexlib.com/Search.aspx?k=phone&AspxAutoDetectCookieSupport=1
TechIsaacIT Contracting

Author

Commented:
Thanks, another issue is my javascript coding.

Thanks for the tip.
TechIsaacIT Contracting

Author

Commented:
Thanks again.

Isaac

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial