You can use positive look ahead just after @
character in your regex to specify the min max length of part that follows @
character. Let's say you want it to be minimum 10 characters and maximum 20, then you can write (?=.{10,20}$)
just after @
in your regex. Here is how your regex should look like,
/^[ ]*[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]{1,64}@(?=.{10,20}$)[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*[ ]*$/;
Here, (?=)
is called positive look ahead and .{10,20}$
means any character minimum 10 and maximum 20 followed by end of string signified by $
You can also refer to this post for getting familiar with how look arounds work.