Class: Israeli::Validators::Phone

Inherits:
Object
  • Object
show all
Defined in:
lib/israeli/validators/phone.rb

Overview

Validates Israeli phone numbers.

Supports three types of Israeli phone numbers:

  • Mobile: 10 digits starting with 05X (050, 052, 053, 054, 055, 058)
  • Landline: 9 digits starting with 0X (02, 03, 04, 08, 09)
  • VoIP: 10 digits starting with 07X (072-079)

Automatically handles international format (+972) conversion.

Examples:

Mobile validation

Israeli::Validators::Phone.valid?("0501234567")              # => true
Israeli::Validators::Phone.valid?("+972501234567")           # => true
Israeli::Validators::Phone.valid?("050-123-4567")            # => true

Landline validation

Israeli::Validators::Phone.valid?("021234567", type: :landline) # => true
Israeli::Validators::Phone.valid?("03-123-4567")                # => true

See Also:

Constant Summary collapse

MOBILE_PATTERN =

Mobile phone pattern: 05X followed by 7 more digits (10 total)

/\A05\d{8}\z/
LANDLINE_PATTERN =

Landline pattern: 0X followed by 7 digits (9 total) Valid area codes: 02 (Jerusalem), 03 (Tel Aviv), 04 (Haifa), 08 (South), 09 (Sharon)

/\A0[2-489]\d{7}\z/
VOIP_PATTERN =

VoIP pattern: 07X (X=2-9) followed by 7 digits (10 total)

/\A07[2-9]\d{7}\z/

Class Method Summary collapse

Class Method Details

.detect_type(value) ⇒ Symbol?

Detects the type of phone number.

Examples:

Israeli::Validators::Phone.detect_type("0501234567")  # => :mobile
Israeli::Validators::Phone.detect_type("021234567")   # => :landline
Israeli::Validators::Phone.detect_type("0721234567")  # => :voip
Israeli::Validators::Phone.detect_type("invalid")     # => nil

Parameters:

  • value (String, nil)

    The phone number to check

Returns:

  • (Symbol, nil)

    :mobile, :landline, :voip, or nil if invalid



87
88
89
90
91
92
93
94
95
96
# File 'lib/israeli/validators/phone.rb', line 87

def self.detect_type(value)
  normalized = Sanitizer.normalize_phone(value)
  return nil if normalized.nil? || normalized.empty?

  return :mobile if mobile?(normalized)
  return :landline if landline?(normalized)
  return :voip if voip?(normalized)

  nil
end

.format(value, style: :dashed) ⇒ String?

Formats a phone number.

Examples:

Phone.format("0501234567")                      # => "050-123-4567"
Phone.format("0501234567", style: :international) # => "+972-50-123-4567"
Phone.format("021234567")                       # => "02-123-4567"

Parameters:

  • value (String, nil)

    The phone number to format

  • style (Symbol) (defaults to: :dashed)

    :dashed, :international, or :compact

Returns:

  • (String, nil)

    Formatted phone number, or nil if invalid



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/israeli/validators/phone.rb', line 132

def self.format(value, style: :dashed)
  normalized = Sanitizer.normalize_phone(value)
  return nil unless valid?(normalized)

  case style
  when :dashed
    format_dashed(normalized)
  when :international
    "+972-#{normalized[1..]}"
  else
    normalized
  end
end

.invalid_reason(value, type: :any) ⇒ Symbol?

Returns the reason why a phone number is invalid.

Examples:

Israeli::Validators::Phone.invalid_reason("abc")         # => :invalid_format
Israeli::Validators::Phone.invalid_reason("021234567", type: :mobile) # => :wrong_type

Parameters:

  • value (String, nil)

    The phone number to check

  • type (Symbol) (defaults to: :any)

    Type to validate: :mobile, :landline, :voip, or :any

Returns:

  • (Symbol, nil)

    Reason code or nil if valid

    • :blank - Input is nil or empty
    • :invalid_format - Does not match any Israeli phone pattern
    • :wrong_type - Valid phone but wrong type (e.g., landline when mobile expected)


110
111
112
113
114
115
116
117
118
119
120
# File 'lib/israeli/validators/phone.rb', line 110

def self.invalid_reason(value, type: :any)
  normalized = Sanitizer.normalize_phone(value)
  return :blank if normalized.nil? || normalized.empty?

  detected = detect_type(normalized)
  return :invalid_format if detected.nil?

  return nil if type == :any || type == detected

  :wrong_type
end

.landline?(value) ⇒ Boolean

Checks if the number is a valid landline number.

Parameters:

  • value (String)

    Normalized phone number

Returns:

  • (Boolean)


65
66
67
# File 'lib/israeli/validators/phone.rb', line 65

def self.landline?(value)
  value.match?(LANDLINE_PATTERN)
end

.mobile?(value) ⇒ Boolean

Checks if the number is a valid mobile number.

Parameters:

  • value (String)

    Normalized phone number

Returns:

  • (Boolean)


57
58
59
# File 'lib/israeli/validators/phone.rb', line 57

def self.mobile?(value)
  value.match?(MOBILE_PATTERN)
end

.valid?(value, type: :any) ⇒ Boolean

Validates an Israeli phone number.

Parameters:

  • value (String, nil)

    The phone number to validate

  • type (Symbol) (defaults to: :any)

    Type to validate: :mobile, :landline, :voip, or :any

Returns:

  • (Boolean)

    true if valid, false otherwise



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/israeli/validators/phone.rb', line 40

def self.valid?(value, type: :any)
  normalized = Sanitizer.normalize_phone(value)
  return false if normalized.nil? || normalized.empty?

  case type
  when :mobile then mobile?(normalized)
  when :landline then landline?(normalized)
  when :voip then voip?(normalized)
  when :any then mobile?(normalized) || landline?(normalized) || voip?(normalized)
  else false
  end
end

.voip?(value) ⇒ Boolean

Checks if the number is a valid VoIP number.

Parameters:

  • value (String)

    Normalized phone number

Returns:

  • (Boolean)


73
74
75
# File 'lib/israeli/validators/phone.rb', line 73

def self.voip?(value)
  value.match?(VOIP_PATTERN)
end