Regular Expression Validation in asp.net displaying the error message even when the phone number is correct

function regex() {
  var regex = new RegExp(/^(009665|9665|+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/);
  regex.test('0501234567'); // return true;
  regex.test('0521234567'); // return false; 
}
 regex()
<div class="newAcc">
  <asp:TextBox ID="phone" runat="server" CssClass="phone" value="+966" placeholder="+966"></asp:TextBox>
  <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="يرجى كتابة رقم هاتف صحيح" CssClass="errorMassege" ControlToValidate="phone" ForeColor="#CC1007" ValidationExpression="/^(009665|9665|+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$/"
    display="Dynamic"></asp:RegularExpressionValidator>
</div>

the regex is working correct and the massage error displays even if the phone number is correct, what is the problem with my code? Also i have added a javascript and jQuery file at the head and didn’t benefit..what can i do please help me ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The / at the beginning and end of the regular expression is JavaScript specific syntax to tell it that this is a regular expression.

In Asp.NET validator your expression should be ValidationExpression="^(009665|9665|+9665|05|5)(5|0|3|6|4|9|1|8|7)([0-9]{7})$"

Note that I removed the / at the beginning and the end of the expression.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x