I am writing a regular expression(regex) for adding multiple email ids in an input box with following conditions:
,
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 in regex101.com and its working like a charm https://regex101.com/r/bU7rU8/1
But when i integrate it with code, it works, but fails on leading and trailing whitespace.
Here is the demo link: http://jsfiddle.net/2G8gA/330/
AngularJS trims the input by default, so you need to use ng-trim="false"
in order to pass leading and trailing whitespace to your pattern
regex.
See documentation:
ngTrim (optional)
If set to
false
Angular will not automatically trim the input. This parameter is ignored forinput[type=password]
controls, which will never trim the input.(default: true)
Do you want leading/trailing spaces allowed on the whole string, or around each individual address?
For the former your regex 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 latter
/^(\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*)*$/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With