【发布时间】:2015-10-28 05:51:30
【问题描述】:
我想检查基本上是用户输入的变量是否是 10 位电话号码。
有 2 组验证: - 如果 num 小于 10 位,则提示 msg - 如果 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。有许多堆栈溢出回答处理不同的问题,但想知道为什么我的代码不起作用。
【问题讨论】:
-
@BradWerth 我想了解为什么我的程序不工作而不是复制其他代码
-
FWIW,params 都以 stings 的形式出现。它们在插入数据库时被强制转换为整数。
-
所以从现有问题中学习,它已经被问过(并回答了)......
标签: ruby-on-rails ruby validation int