有没有办法通过公司电子邮件验证允许用户注册?

时间:2014-11-13 09:36:26

标签: javascript ruby-on-rails regex regex-negation

是否在轨道上使用javascript或ruby进行验证,以允许用户通过公司电子邮件进行注册。公司电子邮件的示例:

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

不允许其他电子邮件服务电子邮件,例如:

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

2 个答案:

答案 0 :(得分:1)

以下是任何电子邮件地址的正则表达式。请注意,连字符可以存在于域名中,因此模式为:

\w+@\w+(?:-\w+)?\.[A-Za-z]{2,4}

这是regex demo

否定前瞻 否定

\w+@(?!(?:gmail|hotmail|yahoo)\.)\w+(?:-\w+)?\.[A-Za-z]{2,4}

这是regex demo


为了提高准确性并将地址与评论中提到的Jan Dvorak等其他字符匹配,这里是典型的RFC 5322电子邮件正则表达式:

(?:[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])+)\])

否定是在粗体区域中完成的:

(?:[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])+)\])

这是regex demo

答案 1 :(得分:0)

除了自己定义正则表达式之外,您还可以使用以下宝石: email_validator

在你的模型中(比如user.rb)

validates :my_email_attribute, :email => true