我想为提交的电子邮件编写验证。但是有一些不同的方法。我将允许用户以“ name'[email protected]”和简单的“ [email protected]”两种格式输入电子邮件。因此,基本上,我想编写一个验证来检查当前的有效电子邮件格式价值与否。
只需要一个自定义验证来检查输入电子邮件值中是否存在有效的电子邮件格式。
我的模型如下:
class Contact < ActiveRecord::Base
validates :email ,presence: true
validate :email_format
def email_format
??? what to write here ???
end
end
我如何为此编写验证。
您需要根据情况稍微修改正则表达式。
validates :email, format: { with: /(\A([a-z]*\s*)*\<*([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\>*\Z)/i }
这将匹配以下格式。
[email protected]
Soundar<[email protected]>
Soundar <[email protected]>
soundar<[email protected]>
Soundar Rathinsamy<[email protected]>
Soundar Rathinsamy <[email protected]>
soundar rathinsamy <[email protected]>
如果您需要更改,请继续在rubular.com上编辑此正则表达式
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句