Sunday, 15 September 2013

javascript - Is there any way to allow user registration via corporate email validation? -



javascript - Is there any way to allow user registration via corporate email validation? -

is there validation in javascript or ruby on rails allow user registration via corporate email. examples of corporate emails:

1) jai@jai.com jai domain name 2) ajay@google.com google.com domain name

and not allow other email service emails, example:

1) jai@gmail.com 2) jai@hotmail.com 3) jai@yahoo.com

here regex email address. note hyphens can nowadays in domain names, hence pattern:

class="lang-regex prettyprint-override">\w+@\w+(?:-\w+)?\.[a-za-z]{2,4}

here regex demo.

you can negate negative lookahead:

class="lang-regex prettyprint-override">\w+@(?!(?:gmail|hotmail|yahoo)\.)\w+(?:-\w+)?\.[a-za-z]{2,4}

here regex demo.

to improve accuracy , match addresses additional characters jan dvorak mentioned in comments, here's typical rfc 5322 email regex:

class="lang-regex prettyprint-override">(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

the negation done in bold area:

class="lang-regex prettyprint-override">(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?!(?:gmail|hotmail|yahoo)\.)(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

here regex demo.

javascript ruby-on-rails regex regex-negation

No comments:

Post a Comment