Class: MobilePhoneValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/validators/mobile_phone_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

验证手机号码

移动号码段: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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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