Regular expression for checking length in email address

We have a text box that accepts an e-mail address separated by a comma. Regular expression

^ (\ w + ([- +.] \ w +) * @ \ w + ([-.] \ w +) * \. \ w + ([-.] \ w +) * \ s * [,]? \ b) * $.

However, now I can not add a length check to this expression. Each email address must be 150 characters or less. I tried

^ ((\ w + ([- +.] \ w +) * @ \ w + ([-.] \ w +) * \. \ w + ([-.] \ w +)) {1,150} \ s * [,]? \ b) * $. 

But that will not work. Can you please change the regular expression so that it also performs this check.

+3
source share
5 answers

, . JavaScript :

function validateEmail(email) {
    var regex = /^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*[,]?\b)*$/;
    // 20 is used instead of 150 for sake of example
    return email.length <= 20 && regex.test(email);
}
// validateEmail("jdoe@example.com") == true
// validateEmail("loooooooooooooooooooonjohn@example.com") == false

, $ , .

+1

, ? , :

^.{1,150}$
+5
^([a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]){0,70}$
0

, ng-pattern Angular.

Taking Rikick's approach to checking lengths separately, I found a much simpler solution: just set ng-maxlength="150"to input. You can then configure an error message that tells the user that the message is too long.

maxlength="150"works great without adding extra characters to the field, but I liked that it ng-maxlengthtells you what is wrong and not truncating your string.

0
source

Use lookahead

/(?=^.{1,150}$)(your regex goes here)/
0
source

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


All Articles