C # Regular expression for checking email in DataAnnotations - double backslash

Saw this code to validate the expression of an email address using data annotations.

I cannot solve the double backslash problem.

They tell me that the letter should contain a backslash, but I know that this is not what it does !!!

 [RegularExpression(".+\\@.+\\..+",   ErrorMessage="Please enter a valid email")]
+3
source share
4 answers

The backslash is an escape character in both C # and the regular expression. So in C #, "\\"equals a single backslash. The resulting backslash is then used to exit from ., which is a metacharacter and therefore must be escaped. I do not know why it @will be saved.

+5

MVC2

using System.ComponentModel.DataAnnotations;

public class EmailValidationAttribute: RegularExpressionAttribute
{    
    public EmailValidationAttribute() : base(@"^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-zA-Z0-9]{1}[a-zA-Z0-9\-]{0,62}[a-zA-Z0-9]{1})|[a-zA-Z])\.)+[a-zA-Z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$")
    {

    }
}

[EmailValidation(ErrorMessage="Not a valid Email Address")]
public string Email { get; set; }

.

+4

. , \d .

# . , \n . #, ... . .

# , - @.

+3

, Escape #. @".+\@.+\..+"

0

Source: https://habr.com/ru/post/1786709/


All Articles