RegExp.test inside selector.each question
I have the code below looping through 2 text fields with PhoneNumber class. I have tried multiple RegEx strings to validate that the user inputs xxx-xxx-xxxx
(using jquery 1.7.1.min) - no console errors
No matter what i try, i still can't get them to match the regEx string. I know there is some javascript, but i hope with the amount of jQuery i am using that i can post this here. Is there something simple I am missing or is my concept wrong or do i just keep using incorrect regEx strings? Any help would be much appreciated.
Thanks,
Eric
JS
- var phoneExpression = new RegExp("\d{3}-\d{3}-\d{4}");
- $('.PhoneNumber').each(function () {
- if (phoneExpression.test($(this).val()) == false) {
- $(this).addClass('MissingData').addClass('HelpTip').prop('title','Format number as xxx-xxx-xxxx');
- isValid = false;
- }
- else {
- $(this).removeClass('MissingData').removeClass('HelpTip').prop('title','');
- }
- });
Markup
- <asp:TextBox ID="txtWorkPhone" runat="server" CssClass="Required PhoneNumber" />
- <asp:TextBox ID="txtHomePhone" runat="server" CssClass="Required PhoneNumber" />