加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rails验证full_name

发布时间:2020-12-16 22:20:06 所属栏目:百科 来源:网络整理
导读:嘿,你如何验证一个full_name字段(姓氏). 解决方法 考虑像: Jan Levinson-Gould女士 马
嘿,你如何验证一个full_name字段(姓氏).

解决方法

考虑像:

> Jan Levinson-Gould女士
>马丁·路德·金博士
> Brett d’Arras-d’Haudracey
>布鲁诺

而不是验证那里的字符,您可能只想确保一些字符集不存在.

例如:

class User < ActiveRecord::Base

  validates_format_of :full_name,:with => /A[^0-9`!@#$%^&*+_=]+z/
  # add any other characters you'd like to disallow inside the [ brackets ]
  # metacharacters [,,^,$,.,|,?,*,+,(,and ) need to be escaped with a 

end

测试

Ms. Jan Levinson-Gould         # pass
Dr. Martin Luther King,Jr.    # pass
Brett d'Arras-d'Haudracey      # pass
Brüno                          # pass
John Doe                       # pass
Mary-Jo Jane Sally Smith       # pass
Fatty Mc.Error$               # fail
FA!L                           # fail
#arold Newm@n                  # fail
N4m3 w1th Numb3r5              # fail

正则表达式解释

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  A                       the beginning of the string
--------------------------------------------------------------------------------
  [^`!@#$%^&*+_=d]+     any character except: '`','!','@','#','$','%','^','&','*','+','_','=',digits (0-9) (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  z                       the end of the string

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读