ruby-on-rails - Rails 正则表达式电子邮件验证,不允许使用空格

标签 ruby-on-rails regex

我目前正在使用此代码进行电子邮件验证:

/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

问题是它仍然接受带有空格的电子邮件,例如 ab c@gmail.com .

我已经尝试了很多不允许空格的代码变体,但不是在表单顶部使用错误消息刷新页面,而是给了我这个错误:

The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?

最佳答案

您缺少开始和结束 anchor 标记,因此您可以引用此链接 Regular expressions with validations in RoR 4你正确的正则表达式将是,

/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i

您也应该尝试使用此正则表达式进行电子邮件验证,此正则表达式将满足您的所有要求并检查所有必需的可能验证。
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$

这是对正则表达式的解释,这将有助于理解验证
^           #start of the line
  [_A-Za-z0-9-\\+]+ #  must start with string in the bracket [ ], must contains one or more (+)
  (         #   start of group #1
    \\.[_A-Za-z0-9-]+   #     follow by a dot "." and string in the bracket [ ], must contains one or more (+)
  )*            #   end of group #1, this group is optional (*)
    @           #     must contains a "@" symbol
     [A-Za-z0-9-]+      #       follow by string in the bracket [ ], must contains one or more (+)
      (         #         start of group #2 - first level TLD checking
       \\.[A-Za-z0-9]+  #           follow by a dot "." and string in the bracket [ ], must contains one or more (+)
      )*        #         end of group #2, this group is optional (*)
      (         #         start of group #3 - second level TLD checking
       \\.[A-Za-z]{2,}  #           follow by a dot "." and string in the bracket [ ], with minimum length of 2
      )         #         end of group #3
$           #end of the line

测试输出:
Email is valid : abc@yahoo.com , true
Email is valid : abc-100@yahoo.com , true
Email is valid : abc.100@yahoo.com , true
Email is valid : abc111@abc.com , true
Email is valid : abc-100@abc.net , true
Email is valid : abc.100@abc.com.au , true
Email is valid : abc@1.com , true
Email is valid : abc@gmail.com.com , true
Email is valid : abc+100@gmail.com , true
Email is valid : abc-100@yahoo-test.com , true
Email is valid : abc , false
Email is valid : abc@.com.my , false
Email is valid : abc123@gmail.a , false
Email is valid : abc123@.com , false
Email is valid : abc123@.com.com , false
Email is valid : .abc@abc.com , false
Email is valid : abc()*@gmail.com , false
Email is valid : abc@%*.com , false
Email is valid : abc..2002@gmail.com , false
Email is valid : abc.@gmail.com , false
Email is valid : abc@abc@gmail.com , false
Email is valid : ab c@gmail.com , false
Email is valid : abc@gmail.com.1a , false

关于ruby-on-rails - Rails 正则表达式电子邮件验证,不允许使用空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36026590/

相关文章:

ruby-on-rails - 使用 Heroku 和 CloudFront 从 RapidSSL 获得的 SSL 证书

ruby-on-rails - rails gem 'countries' - LoadError : cannot load such file -- iso3166

mysql - 从 MySQL 转储中提取 CREATE TABLE 定义?

javascript - 检查字符串是否为 Spotify URL

JavaScript 正则表达式不匹配

regex - 在Perl中,如何匹配两个连续的回车符?

ruby-on-rails - Uglifier::错误:意外的标记 punc «;»,预期的 punc «,»

ruby-on-rails - 使用rails迁移生成器添加外键

ruby-on-rails - Rails 默认只大写第一个单词的第一个字母,而不是我输入的字符串

regex - GET-请求中的具体参数 : don't doing 301 redirect