0

How is it possible to validate corporate emails in Validate.js plugin? To prevent users from entering a non-corporate emails. Of emails like @gmail.com, @yahoo.com, @hotmail.com.

That's the part of email validation in Validate.js main script:

email: function( value, element ) {
            return this.optional( element ) || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test( value );
        }
Sparky
  • 98,165
  • 25
  • 199
  • 285
CoreDo
  • 2,691
  • 4
  • 17
  • 18
  • 2
    If you have a definite set of not allowed domains (or allowed), it'd be easier. – lshettyl Apr 11 '15 at 08:15
  • I've a set of not allowed domains. As I mentioned, Gmail, yahoo, hotmail. – CoreDo Apr 11 '15 at 09:31
  • Do you care about <= IE8 ? – lshettyl Apr 11 '15 at 10:47
  • Yeah. HTML5 wouldn't fit here! – CoreDo Apr 11 '15 at 10:51
  • **You should not be editing the source code of the plugin.** Instead, use the custom function from the answer below for making a custom email rule with the `.addMethod()` method. See: http://stackoverflow.com/questions/241145/jquery-validate-plugin-how-to-create-a-simple-custom-rule – Sparky Apr 11 '15 at 14:39

2 Answers2

2

A clean way to solve this is to extract the domain name into a variable (by taking a substring after the @) and then to check the value against an array of domains.

Doing this in a regex is creating a nightmare for whoever is going to read this code in the future!

Example:

var email = "name@example.com";
var domainName = email.slice((email.indexOf("@") + 1), email.length);

and then check if domainName is in the list of domains.

Ilya Kogan
  • 21,995
  • 15
  • 85
  • 141
0

Add the exceptions and validate against them..

In your case, you can use multile regex and validate against them like this..

  var regex1 =   what-ever-regex-is-@yahoo.com
  var regex2 =   what-ever-regex-is-@hotmail.com
  var regex3 =   what-ever-regex-is-@gmail.com
  var regex4 =   what-ever-regex-is-@live.com

and use ! in-front of return..that means if one of them would be existing, your check would return true but ! will change the sign so that will be false and use it in your code accordingly.

 email: function( value, element ) {
        return (this.optional( element ) || 
 /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@yahoo.com$/.test( value ) 
    || /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@hotmail.com$/.test( value )  );
    }

Another way could be to merge all of these knows domains in one regex and check against it.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
  • @AmroShahbari can you you see I have modified your function..changed your function to multiple functions.. – Danyal Sandeelo Apr 11 '15 at 08:47
  • so you need to add rest of them as well like I have done .. it should accept hotmail and yahoo only.. you need to add more – Danyal Sandeelo Apr 11 '15 at 08:49
  • Code should prevent Yahoo and hotmail. not accept them! – CoreDo Apr 11 '15 at 08:51
  • yes.. if the check is TRUE...that means it contains one of these emails alert the invalid message..that is like solving it via contradiction – Danyal Sandeelo Apr 11 '15 at 08:53
  • 1
    @AmroShahbari dont add regex 1 2 or 3.. use the second part of the code i have written .. the regexes are already there.. – Danyal Sandeelo Apr 11 '15 at 09:31
  • @DanyelSandeelo The problem that id doesn't work. Still accepting yahoo.com and hotmail.com emails. – CoreDo Apr 11 '15 at 09:34
  • ofcourse, it will change your code to return !(this.optional( element ) ---> add ! – Danyal Sandeelo Apr 11 '15 at 09:44
  • **You should not be editing the source code of the plugin.** Instead, use this function for making a custom email rule with the `.addMethod()` method. See: http://stackoverflow.com/questions/241145/jquery-validate-plugin-how-to-create-a-simple-custom-rule – Sparky Apr 11 '15 at 14:39