1

I want to validate the entered email id domain using jQuery. For security reason we restrict the email id to only 2 email domain like company1.com and **company2.co, **.

So, if the user enters user1@gmail.com then we need to display the error message enter a valid email address.

The user is allowed to enter only like user1@company1.com or user2@company2.com.

I am using the below code but it is not working:

jQuery.validator.addMethod("domain", function(
            value, element) {
            var val = value.substring(value.indexOf('@')+1, value.length);
            return this.optional(element) || /'company1.com'/.test(val) || /'company2.com'/.test(val);              

    }, 'Enter a valid email address');

In rules I am adding domain: true

could you please tell me where I missed?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Arat Kumar rana
  • 187
  • 1
  • 5
  • 19

3 Answers3

2

The regex is wrong, you've added quotes and haven't escaped the period, which has special meaning in a regex, it should look like

/company1\.com/.test(val)

but it looks like there's an easier to way to do this without regex

jQuery.validator.addMethod("domain", function (value, element) {
    return this.optional(element) || 
           ['company1.com','company2.com'].indexOf(value.split('@').pop()) != -1;
},'Enter a valid email address');
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • I used as per your suggestion. But if I enter user@company1.com, it also shows the error message – Arat Kumar rana Nov 15 '14 at 06:19
  • And you're sure something else isn't wrong? What happens if you remove `this.optional(element)` ? – adeneo Nov 15 '14 at 06:23
  • If remove `this.optional(element)` same error showing. – Arat Kumar rana Nov 15 '14 at 06:27
  • I think this condition does not satisfy `['company1.com','company2.com'].indexOf(value.split('@').pop()) != -1` – Arat Kumar rana Nov 15 '14 at 06:33
  • Then I have no idea, it works perfectly fine for me -> **http://jsfiddle.net/adeneo/tcm4f9gd/** – adeneo Nov 15 '14 at 06:34
  • I got it. I was using upper case `['COMPANY1.COM','COMPANY2.COM'].indexOf(value.split('@').pop()) != -1`. If I use small case `['company1.com','company2.com'].indexOf(value.split('@').pop()) != -1` it is working fine. Could you tell me how to use ignoreCase here? – Arat Kumar rana Nov 15 '14 at 06:45
  • Sure, `['company1.com','company2.com'].indexOf(value.split('@').pop().toLowerCase()) != -1` – adeneo Nov 15 '14 at 06:47
0

You can try this regular expression:

emailRegex = /^[a-zA-Z0-9.!#$%&'+/=?^_{|}~-]+@company1\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/ and emailRegex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_{|}~-]+@company2.a-zA-Z0-9?)$/

Vipul Hadiya
  • 882
  • 1
  • 11
  • 22
0

function isValidEmailAddress(emailAddress) { var pattern = new RegExp(/([\w-.]+)@((?:[company1]+.)+)([a-zA-Z]{2,4})/i); return pattern.test(emailAddress); };

call function

isValidEmailAddress('user1@company1.com')

if( !isValidEmailAddress( emailaddress ) ) { /* do stuff here */ }

chack this link Validating email addresses using jQuery and regex

Community
  • 1
  • 1
VIKASH DASH
  • 543
  • 1
  • 5
  • 12