Rails 4 表单验证正则表达式(标题,body)
Rails 4 form validate regex (title, body)
正在验证微博标题和body。我一直在努力弄清楚这一点。越来越个性化了...:)
我对 Rails(1.5 个月)和正则表达式(1 天)都是新手
对于标题:我想允许 UTF-8 字符和 spaces
对于 body:我想允许 UTF-8 字符、spaces 和标点符号。
http://rubular.com/ (ruby v 2.1.5) 告诉我这没问题:
/[[:space:]]*[[:alpha:]]*/
/[[:alpha:]]*[[:space:]]*/
但是当我尝试这个时:
validates :title, presence: true, length: { minimum: 10, maximum: 60 }, format: { with: /[[:alpha:]]*[[:space:]]*/, message: "only letters" }
它让像这样的数字和字符 +!%/=( 溜走。
以下也失败了。它不允许我 spaces - 至少当我包含数字或其他奇怪字符时它会引发错误:
format: { with: /\A[[:alpha:]]*[[:space:]]*\z/, message: "only letters" }
我也试过做这样的东西,但没什么区别,也失败了:
REGEX = ........
format: { with: REGEX, message: "only letters" }
这些也失败了:
format: { with: /\A[\p{L}\ ]\z/
format: { with: /[\p{L}\ ]/
提前致谢!
编辑
我的模特 idea.rb
class Idea < ActiveRecord::Base
belongs_to :user
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
attr_reader :tag_tokens
acts_as_likeable
has_many :likes, foreign_key: :likeable_id
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :title, presence: true, length: { minimum: 10, maximum: 60 }, format: { with: /[[:alpha:]]|[[:space:]]/, message: 'only letters'}
validates :intro, presence: true, length: { minimum: 60, maximum: 160 }
validates :tag_ids, presence: true
....
private
def idea_params
params.require(:idea).permit(:title, :intro, :content, :user_id, :name)
end
end
这不允许在开头使用数字,但允许在其他任何地方使用:
validates :title, presence: true, length: { minimum: 10, maximum: 60 }, format: { with: /\A[[:alpha:]]|[[:space:]]\z/, message: 'only letters'}
这不允许 space:
/\A([[:alpha:]]|[[:space:]]+)\z/
这允许 space 但也允许数字:
/([[:alpha:]]|[[:space:]]+)/
也接受数字:
/(\p{L} +)/
/[\p{L}\s]+/
我从 Ruby language documentation site.
获取了有关 utf-8 字符正则表达式的信息
为我工作
# /[[:alpha:]]|[[:space:]]/
class Selection
include Mongoid::Document
field :name, type: String
validates :name, presence: true, format: { with: /[[:alpha:]]|[[:space:]]/, message: 'only letters'}
end
但是当你使用 presence 选项时,这意味着你不能只有空格。
您可以使用
进行测试
~
解决方法是:
/\A[\p{L}\s]+\z/
这接受任何 unicode (UTF-8) 字母和任何数量的 space 但不接受数字或其他字符(例如:%/=()+!)在单词中的任何位置或单独使用。
终于……:)
正在验证微博标题和body。我一直在努力弄清楚这一点。越来越个性化了...:)
我对 Rails(1.5 个月)和正则表达式(1 天)都是新手
对于标题:我想允许 UTF-8 字符和 spaces 对于 body:我想允许 UTF-8 字符、spaces 和标点符号。
http://rubular.com/ (ruby v 2.1.5) 告诉我这没问题:
/[[:space:]]*[[:alpha:]]*/
/[[:alpha:]]*[[:space:]]*/
但是当我尝试这个时:
validates :title, presence: true, length: { minimum: 10, maximum: 60 }, format: { with: /[[:alpha:]]*[[:space:]]*/, message: "only letters" }
它让像这样的数字和字符 +!%/=( 溜走。
以下也失败了。它不允许我 spaces - 至少当我包含数字或其他奇怪字符时它会引发错误:
format: { with: /\A[[:alpha:]]*[[:space:]]*\z/, message: "only letters" }
我也试过做这样的东西,但没什么区别,也失败了:
REGEX = ........
format: { with: REGEX, message: "only letters" }
这些也失败了:
format: { with: /\A[\p{L}\ ]\z/
format: { with: /[\p{L}\ ]/
提前致谢!
编辑 我的模特 idea.rb
class Idea < ActiveRecord::Base
belongs_to :user
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
attr_reader :tag_tokens
acts_as_likeable
has_many :likes, foreign_key: :likeable_id
default_scope -> { order(created_at: :desc) }
validates :user_id, presence: true
validates :title, presence: true, length: { minimum: 10, maximum: 60 }, format: { with: /[[:alpha:]]|[[:space:]]/, message: 'only letters'}
validates :intro, presence: true, length: { minimum: 60, maximum: 160 }
validates :tag_ids, presence: true
....
private
def idea_params
params.require(:idea).permit(:title, :intro, :content, :user_id, :name)
end
end
这不允许在开头使用数字,但允许在其他任何地方使用:
validates :title, presence: true, length: { minimum: 10, maximum: 60 }, format: { with: /\A[[:alpha:]]|[[:space:]]\z/, message: 'only letters'}
这不允许 space:
/\A([[:alpha:]]|[[:space:]]+)\z/
这允许 space 但也允许数字:
/([[:alpha:]]|[[:space:]]+)/
也接受数字:
/(\p{L} +)/ /[\p{L}\s]+/
我从 Ruby language documentation site.
获取了有关 utf-8 字符正则表达式的信息为我工作
# /[[:alpha:]]|[[:space:]]/
class Selection
include Mongoid::Document
field :name, type: String
validates :name, presence: true, format: { with: /[[:alpha:]]|[[:space:]]/, message: 'only letters'}
end
但是当你使用 presence 选项时,这意味着你不能只有空格。
您可以使用
进行测试~
解决方法是:
/\A[\p{L}\s]+\z/
这接受任何 unicode (UTF-8) 字母和任何数量的 space 但不接受数字或其他字符(例如:%/=()+!)在单词中的任何位置或单独使用。
终于……:)