Ruby on rails 使用正则表达式ruby验证电话号码
我正在尝试验证电话号码是否为数字:- 这是我的user.rgRuby on rails 使用正则表达式ruby验证电话号码,ruby-on-rails,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3.1,我正在尝试验证电话号码是否为数字:- 这是我的user.rg number_regex = /\d[0-9]\)*\z/ validates_format_of :phone, :with => number_regex, :message => "Only positive number without spaces are allowed" 这是我的view.html.haml %li %strong=f.label :phone, "Phone Number"
number_regex = /\d[0-9]\)*\z/
validates_format_of :phone, :with => number_regex, :message => "Only positive number without spaces are allowed"
这是我的view.html.haml
%li
%strong=f.label :phone, "Phone Number"
=f.text_field :phone, :placeholder => "Your phone number"
这是控制器
def edit_profile
@user = current_user
request.method.inspect
if request.method == "POST"
if @user.update_attributes(params[:user])
sign_in(@user, :bypass => true)
flash[:success] = "You have updated your profile successfully"
redirect_to dashboard_index_path
else
flash[:error] = "Profile could not be updated"
render :action => "edit_profile"
end
end
end
当我第一次在文本字段中输入数字时,它会进行验证,但是如果我输入正确的格式,然后尝试输入错误的格式,它会跳过验证,并且我会收到一条闪光消息,说明配置文件已成功更新,但不会保存错误的值(带字母)
这里可能有什么问题?我用这个:with=>“没问题”
如果你想要一条信息,(不是按摩),试试这个
validates :phone, :presence => {:message => 'hello world, bad operation!'},
:numericality => true,
:length => { :minimum => 10, :maximum => 15 }
同时检查问题。试试这个:
validates_format_of :phone, :with => /\d[0-9]\)*\z/ , :message => "Only positive number without spaces are allowed"
至少对字段进行一点清理。当我收到像“禁止空白”这样的信息时,我就像是“又是noobs”。如果我第一次尝试输入字母表,这会起作用。但如果在保存1234后输入say“1234lkgfdgdkj”,则表明配置文件已成功更新。如果您从控制台检查电话号码,该值仍然是1234,但我没有收到flash[:error]
validates_format_of :phone, :with => /\d[0-9]\)*\z/ , :message => "Only positive number without spaces are allowed"