Ruby:验证给定的用户输入是否为数字
Ruby: Verifying if the given user input is a number
我想检查基本上是用户输入的变量是否是 10 位 phone 数字。
有两组验证:
- 如果 num 小于 10 位,则提示一条消息
- 如果 num 是字符串而不是整数
@phone = params[:phone_num]
puts "phone_num: #{@phone}"
if @phone.is_a? Integer
puts "phone_num is int"
if @phone.to_s.length == 10
puts "10 digit"
perform(@phone)
@output = "Valid Number, will receive a call"
end
else
puts "Wont be calling"
@output = "The number is invalid"
end
无论我在文本框中输入什么,我得到的输出始终是 The number is invalid
。有许多堆栈溢出回答处理不同的问题,但想知道为什么我的代码不起作用。
The output that I get is always The number is invalid no matter what I
enter in text box.
你的代码总是退回到 else
部分的原因是因为来自 params
的值总是 strings。所以params[:phone_num]
的值是一个字符串。所以你的代码在这里失败 if @phone.is_a? Integer
。相反,您需要将其更改为 params[:phone_num].to_i
@phone = params[:phone_num].to_i
puts "phone_num: #{@phone}"
if @phone.is_a? Integer
puts "phone_num is int"
if @phone.to_s.length == 10
puts "10 digit"
perform(@phone)
@output = "Valid Number, will receive a call"
end
else
puts "Wont be calling"
@output = "The number is invalid"
end
注:
是的。这是执行验证的糟糕方法。我只是在回答 OP 的问题。
看看这个 - A comprehensive regex for phone number validation - 如何确定一个字符串看起来像一个 phone 数字。有一个非常复杂的正则表达式,因为人们有多种形式来输入 phone 个数字!
我个人不喜欢超级复杂的正则表达式,但这正是它们的发明目的。所以这是当你想弄清楚什么样的形式是可以接受的,写一些测试,让你的代码通过上面大量的 link 验收!
编辑:您的代码在很多地方都是错误的; params 已经是一个字符串,所以试试这个!还要记住你的嵌套 if/else/end。
@phone = params[:phone_num]
if @phone =~ /\A\d+\Z/ # replace with better regex
# this just means "string is all numbers"
puts "phone_num is int"
if @phone.length == 10
puts "10 digit"
perform(@phone)
@output = "Valid Number, will receive a call"
else
puts "Number length wrong, #{@phone.length}"
end
else
puts "Wont be calling, not a number: #{@phone.inspect}"
@output = "The number is invalid"
end
对此有标准验证(length
) & (numericality
):
#app/models/user.rb
class User < ActiveRecord::Base
validates :phone_num, length: { is: 10 }, numericality: { only_integer: true }
end
这种类型的验证属于模型。
备注
您的控制器将如下所示:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def create
@user = User.new user_params
@user.save #-> validations handled by model
end
end
有一个原则叫做 fat model, skinny controller - 你应该在你的模型中加入 "data" 逻辑。
这样做的原因是为了从控制器中删除低效代码。
它让你能够委托你的大部分逻辑给Rails核心助手(例如validations
),而不是调用你自己的前端有大量代码(就像你正在做的那样)。
每次您 运行 一个 Rails 应用程序时,各种 类(控制器和模型)都会加载到内存中。除了所有 Rails 类(ActiveRecord
等),您的控制器和模型也必须加载。
任何 额外的代码都会导致 bloat,使您的应用程序出现错误且无法使用。最好的开发人员知道什么时候使用自己的代码,什么时候委托给 Rails。这个例子完美地展示了何时委托。
我想检查基本上是用户输入的变量是否是 10 位 phone 数字。
有两组验证: - 如果 num 小于 10 位,则提示一条消息 - 如果 num 是字符串而不是整数
@phone = params[:phone_num]
puts "phone_num: #{@phone}"
if @phone.is_a? Integer
puts "phone_num is int"
if @phone.to_s.length == 10
puts "10 digit"
perform(@phone)
@output = "Valid Number, will receive a call"
end
else
puts "Wont be calling"
@output = "The number is invalid"
end
无论我在文本框中输入什么,我得到的输出始终是 The number is invalid
。有许多堆栈溢出回答处理不同的问题,但想知道为什么我的代码不起作用。
The output that I get is always The number is invalid no matter what I enter in text box.
你的代码总是退回到 else
部分的原因是因为来自 params
的值总是 strings。所以params[:phone_num]
的值是一个字符串。所以你的代码在这里失败 if @phone.is_a? Integer
。相反,您需要将其更改为 params[:phone_num].to_i
@phone = params[:phone_num].to_i
puts "phone_num: #{@phone}"
if @phone.is_a? Integer
puts "phone_num is int"
if @phone.to_s.length == 10
puts "10 digit"
perform(@phone)
@output = "Valid Number, will receive a call"
end
else
puts "Wont be calling"
@output = "The number is invalid"
end
注:
是的。这是执行验证的糟糕方法。我只是在回答 OP 的问题。
看看这个 - A comprehensive regex for phone number validation - 如何确定一个字符串看起来像一个 phone 数字。有一个非常复杂的正则表达式,因为人们有多种形式来输入 phone 个数字!
我个人不喜欢超级复杂的正则表达式,但这正是它们的发明目的。所以这是当你想弄清楚什么样的形式是可以接受的,写一些测试,让你的代码通过上面大量的 link 验收!
编辑:您的代码在很多地方都是错误的; params 已经是一个字符串,所以试试这个!还要记住你的嵌套 if/else/end。
@phone = params[:phone_num]
if @phone =~ /\A\d+\Z/ # replace with better regex
# this just means "string is all numbers"
puts "phone_num is int"
if @phone.length == 10
puts "10 digit"
perform(@phone)
@output = "Valid Number, will receive a call"
else
puts "Number length wrong, #{@phone.length}"
end
else
puts "Wont be calling, not a number: #{@phone.inspect}"
@output = "The number is invalid"
end
对此有标准验证(length
) & (numericality
):
#app/models/user.rb
class User < ActiveRecord::Base
validates :phone_num, length: { is: 10 }, numericality: { only_integer: true }
end
这种类型的验证属于模型。
备注
您的控制器将如下所示:
#app/controllers/users_controller.rb
class UsersController < ApplicationController
def create
@user = User.new user_params
@user.save #-> validations handled by model
end
end
有一个原则叫做 fat model, skinny controller - 你应该在你的模型中加入 "data" 逻辑。
这样做的原因是为了从控制器中删除低效代码。
它让你能够委托你的大部分逻辑给Rails核心助手(例如validations
),而不是调用你自己的前端有大量代码(就像你正在做的那样)。
每次您 运行 一个 Rails 应用程序时,各种 类(控制器和模型)都会加载到内存中。除了所有 Rails 类(ActiveRecord
等),您的控制器和模型也必须加载。
任何 额外的代码都会导致 bloat,使您的应用程序出现错误且无法使用。最好的开发人员知道什么时候使用自己的代码,什么时候委托给 Rails。这个例子完美地展示了何时委托。