我有以下正则表达式,我在routes.rb中使用/ type-in-something-here
# A-Z, a-z, 0-9, _ in the middle but never starting or ending in a _
# At least 5, no more than 500 characters
Run Code Online (Sandbox Code Playgroud)
在路线中,这适用于:
match ':uuid' => 'room#show', :constraints => { :uuid => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/ }
Run Code Online (Sandbox Code Playgroud)
我想将此作为验证,因此不会创建无效记录.所以我在room.rb中添加了以下内容:
validates_format_of :uuid, :with => /[A-Za-z\d]([-\w]{,498}[A-Za-z\d])?/i, :message => "Invalid! Alphanumerics only."
Run Code Online (Sandbox Code Playgroud)
但是这个validates_format_of不起作用,而不是添加错误,它允许记录保存.
有什么想法有什么不对吗?
谢谢
我正在使用Ruby on Rails 3.0.9,我想验证一个只能包含字符(不是特殊字符 - 不区分大小写),空格和数字的字符串.
在我的验证码中,我有:
validates :name,
:presence => true,
:format => { :with => regex } # Here I should set the 'regex'
Run Code Online (Sandbox Code Playgroud)
我该如何陈述正则表达式?