1

I need to validations in place for my Angular app on an email field. The restrictions are: 1. The email should be of valid format (Angular's built in validators would take care of this) 2. The email should have a fixed '@abc.com' part. The field should error out in case someone enters another domain.

I'm thinking I should use ng-pattern, but am not sure how to go forward with it. TIA!

nikjohn
  • 20,026
  • 14
  • 50
  • 86

2 Answers2

1
/^[-_A-Za-z0-9\.]+@abc\.com$/g    

Quantifiers :

+ Between one and unlimited times, as many times as possible, giving back as needed [greedy]

-_ a single character in the list -_ literally

A-Z a single character in the range between A and Z (case sensitive)

a-z a single character in the range between a and z (case sensitive)

0-9 a single character in the range between 0 and 9

@abc matches the characters @abc literally (case sensitive)

\. matches the character . literally

com matches the characters com literally (case sensitive) $ assert position at end of the string

g modifier: global. All matches (don't return on first match)

mohamedrias
  • 18,326
  • 2
  • 38
  • 47
0

A regular expression is quite heavy machinery for this purpose. Just split the string containing the email address at the @ character, and take the second half. (An email address is guaranteed to contain only one @ character.)

But if anyway you want to use it:

@(.*)$

This will match with the @, then capture everything up until the end of input ($)

Check here

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109