nikjohn
nikjohn

Reputation: 21908

JavaScript regular expression for emails with fixed '@abc.com'

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!

Upvotes: 1

Views: 624

Answers (2)

mohamedrias
mohamedrias

Reputation: 18576

/^[-_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)

Upvotes: 1

Jordi Castilla
Jordi Castilla

Reputation: 27003

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

Upvotes: 0

Related Questions