locked
Error with email Regular Expression Validator RRS feed

  • Question

  • User-1994446809 posted

    Hello Forum,

    I tried to make my email textbox to accept valid email address with Regular Expression Validator. But when I insert a valid email address, the validator error message still shows that the email is not valid. Please how can I resolve this?

    HERE IS THE VALIDATION EXPRESSION I USED:

    <asp:TextBox ID="mailtxtbx" runat="server" CssClass="form-control" placeholder="Email Address" />
    <asp:RegularExpressionValidator ID="Regex" runat="server" Font-Size="X-Small" ErrorMessage="Please provide a Valid Mail Address" Font-Bold="true" ValidationExpression="/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;" ControlToValidate="mailtxtbx" ForeColor="Red"></asp:RegularExpressionValidator>
    

    Monday, September 7, 2020 12:46 PM

Answers

  • User1535942433 posted

    Hi georgeakpan233,

    Accroding to your codes,as far as I think,the validationExpression in RegularExpressionValidator control is different with in jquery.

    You could replace:

    ValidationExpression="/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;"

    With:

    ValidationExpression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$;"

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 8, 2020 2:27 AM

All replies

  • User409696431 posted

    Use a valid expression for email addresses.  The one you use doesn't appear to work for any email address I try.

    Test your expressions here: http://regexstorm.net/tester

    Here is one that works, with strong caveats.

    \A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z

    The caveats: you can find many examples on the web for Regex email matching, but the ones that fully match all possible valid emails are very, very long. You may find valid email addresses that the above version doesn't match.

    Monday, September 7, 2020 8:53 PM
  • User1535942433 posted

    Hi georgeakpan233,

    Accroding to your codes,as far as I think,the validationExpression in RegularExpressionValidator control is different with in jquery.

    You could replace:

    ValidationExpression="/^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;"

    With:

    ValidationExpression="^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$;"

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 8, 2020 2:27 AM