AngularJS - remove spaces in upper and back space from input field using regular expression

I am writing a regex to add multiple email identifiers to an input field with the following conditions:

  • Multiple email identifiers must be separated by a comma ,
  • You must have at least one email id
  • There should be no spaces in the input field.

So, I created this regex:

 ^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([,.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$ 

I tested it at regex101.com and worked like a charm https://regex101.com/r/bU7rU8/1

But when I integrate it with the code, it works , but it does not work with leading and trailing spaces .

Here is a demo link: http://jsfiddle.net/2G8gA/330/

+5
source share
2 answers

AngularJS by default portes input, so you need to use ng-trim="false" to pass leading and trailing spaces to the pattern regular expression.

See documentation :

ngTrim (optional)

If false Angular will not automatically crop the input. This parameter is ignored for input[type=password] controls that will never trim the input.

(default: true)

+10
source

Do you want leading / trailing spaces valid for the entire line or around each individual address?

For your first regular expression should be

 /^(\s*([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([,.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+\s*)*$/ 

and for the last

 /^(\s*([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25}\s*)+([,.](\s*([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+\s*)*$/ 
+2
source

All Articles