class YolCommon::Validator
Public Class Methods
asset_file_ext?(file_name)
click to toggle source
静态文件后缀
# File lib/yol_common/validator.rb, line 43 def self.asset_file_ext?(file_name) name = file_name.to_s.downcase ext = File.extname(name) return false if ext.empty? %w{.eot .woff .woff2 .ttf .svg .js .css .htc .ico .json .otf}.include?(ext) end
asset_file_name?(file_name)
click to toggle source
静态文件名
# File lib/yol_common/validator.rb, line 35 def self.asset_file_name?(file_name) ext = File.extname(file_name) file_name = File.basename(file_name, ext) unless ext.empty? return true if file_name =~ /^[\w\.\-\_]+$/ false end
bank_card?(card)
click to toggle source
银行卡
# File lib/yol_common/validator.rb, line 154 def self.bank_card?(card) return true if card.to_s =~ /^\d{9,20}$/ return false end
date?(date)
click to toggle source
日期
# File lib/yol_common/validator.rb, line 97 def self.date?(date) begin Date.parse(date) return true rescue return false end end
domain_name?(name)
click to toggle source
域名
# File lib/yol_common/validator.rb, line 66 def self.domain_name?(name) return false if name.length < 1 || name.length > 63 return true if name.to_s =~ /^[a-z0-9-]+$/ false end
email?(email)
click to toggle source
邮箱
# File lib/yol_common/validator.rb, line 15 def self.email?(email) return false if email.length <= 3 || email.length >= 255 # email如包含\xBF则会报错:invalid byte sequence in UTF-8,在ruby2.1可以用scrub函数去掉错误编码的字符 return true if email.to_s =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i rescue nil false end
float?(float)
click to toggle source
float
# File lib/yol_common/validator.rb, line 117 def self.float?(float) begin Float(float) return true rescue return false end end
folder_name?(str)
click to toggle source
方法说明
# File lib/yol_common/validator.rb, line 171 def self.folder_name?(str) return true if str.to_s =~ /^[^\\\/:*?"<>|]{1,}$/ false end
icon?(file_name)
click to toggle source
icon
# File lib/yol_common/validator.rb, line 81 def self.icon?(file_name) name = file_name.to_s.downcase ext = File.extname(name) return false if ext.empty? %w{.ico}.include?(ext) end
image?(file_name)
click to toggle source
图片类型
# File lib/yol_common/validator.rb, line 73 def self.image?(file_name) name = file_name.to_s.downcase ext = File.extname(name) return false if ext.empty? %w{.jpg .jpeg .gif .png .bmp .webp .ico}.include?(ext) end
image_asset_file_name?(file_name)
click to toggle source
图片文件名字
# File lib/yol_common/validator.rb, line 51 def self.image_asset_file_name?(file_name) ext = File.extname(file_name) file_name = File.basename(file_name, ext) unless ext.empty? return true if file_name =~ /^[\w\-\_]+$/ false end
indentity_card?(card)
click to toggle source
验证身份证号码
# File lib/yol_common/validator.rb, line 127 def self.indentity_card?(card) arr_int = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] arr_ch = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] case card.size when 15 chars = card.chars.to_a cardtemp_15 = 0 chars.insert(6,'1','9') 17.times do |i| cardtemp_15 += (chars[i].to_i * arr_int[i]) end chars << arr_ch[cardtemp_15 % 11] when 18 chars = card.chars.to_a else return false end cardtemp = 0 17.times do |i| cardtemp += chars[i].to_i * arr_int[i] end arr_ch[cardtemp % 11].eql?(chars.last) end
integer?(integer)
click to toggle source
整数
# File lib/yol_common/validator.rb, line 107 def self.integer?(integer) begin Integer(integer) return true rescue return false end end
is_mobile_agent?(agent)
click to toggle source
手机
# File lib/yol_common/validator.rb, line 9 def self.is_mobile_agent?(agent) return true if agent.to_s.downcase =~ MOBILE_REGEX false end
json?(file_name)
click to toggle source
json
# File lib/yol_common/validator.rb, line 89 def self.json?(file_name) name = file_name.to_s.downcase ext = File.extname(name) return false if ext.empty? ['.json'].include?(ext) end
max_length?(target, length = 255)
click to toggle source
方法说明
# File lib/yol_common/validator.rb, line 186 def self.max_length?(target, length = 255) target.to_s.length > length end
min_length?(target, length = 10)
click to toggle source
方法说明
# File lib/yol_common/validator.rb, line 191 def self.min_length?(target, length = 10) target.to_s.length < length end
mobile?(mobile)
click to toggle source
手机
# File lib/yol_common/validator.rb, line 23 def self.mobile?(mobile) return true if mobile.to_s =~ /^(13|14|15|16|17|18|19)\d{9}$/ false end
password?(pwd)
click to toggle source
密码
# File lib/yol_common/validator.rb, line 29 def self.password?(pwd) return true if pwd.length >=4 && pwd.length <= 40 false end
positive_integer?(str)
click to toggle source
方法说明
@param 参数说明 @return [返回数据类型] 返回说明
# File lib/yol_common/validator.rb, line 180 def self.positive_integer?(str) return true if str.to_s =~ /^(0|[1-9]\d*)$/ false end
price?(str)
click to toggle source
方法说明
# File lib/yol_common/validator.rb, line 165 def self.price?(str) return true if str.to_s =~ /^[0-9]+([.]{1}[0-9]{1,2})?$/ false end
under_size?(size_in_byte, limit_mb)
click to toggle source
方法说明
# File lib/yol_common/validator.rb, line 160 def self.under_size?(size_in_byte, limit_mb) size_in_byte <= limit_mb * 1024 * 1024 end
url?(url)
click to toggle source
url
# File lib/yol_common/validator.rb, line 59 def self.url?(url) # return true if url.to_s =~ /https?:\/\/[\S]+/ return true if url.to_s.scan(URI.regexp).length > 0 false end