class MobilePhoneValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source

验证手机号码

移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147 联通号码段:130、131、132、136、185、186、145 电信号码段:133、153、180、189

# File lib/validators/mobile_phone_validator.rb, line 7
def validate_each(record, attribute, value)
  return if options[:allow_blank] && value.blank?
  format = %r{\A1
    (
      (3[123456789])|
      (4[57])|
      (5[0123789])|
      (8[02356789])
    )
    \d{8}
    \z
  }x
  record.errors.add attribute, :invalid unless format =~ value.to_s
end