gpt4 book ai didi

ruby-on-rails - 使用正则表达式 ruby 验证电话号码

转载 作者:行者123 更新时间:2023-12-03 16:17:59 25 4
gpt4 key购买 nike

我正在尝试验证电话号码是否为数字:-

这是我的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"
=f.text_field :phone, :placeholder => "Your phone number"

这是 Controller
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 => true,
:numericality => true,
:length => { :minimum => 10, :maximum => 15 }

如果您想要消息 (不是MASSAGE),请尝试以下操作,
 validates :phone,   :presence => {:message => 'hello world, bad operation!'},
:numericality => true,
:length => { :minimum => 10, :maximum => 15 }

还要检查 this问题。

关于ruby-on-rails - 使用正则表达式 ruby 验证电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10515381/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com