Link to home
Start Free TrialLog in
Avatar of Isaac Johnson
Isaac JohnsonFlag for United States of America

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of Robb Hill
Robb Hill
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac Johnson

ASKER

Thanks, another issue is my javascript coding.

Thanks for the tip.
Thanks again.

Isaac