Mon 31 Mar 2008
Regular expression checking of landline/mobile UK numbers in Ruby on Rails
Posted by kosmas under ruby on rails
Problem
You want to have validations for phone numbers in Ruby on Rails code.
Solution
The mobile numbers in UK should start with a 0, followed by 7, then a number between 5 and 9, and finally another 8 numbers.
For the landline numbers in UK the number should start with 0, followed by either 1 and 8 or 9 digits, or by 2 or 3 and then 9 digits.
So in order to use a regular expression in your validations, you could use something like the following in your model:
case phone_no_type
when 'mobile'
if phone_no !~ /\A(([0][7][5-9])(\d{8}))\Z/
errors.add('Mobile number")
end
when 'landline'
if phone_no !~ /\A([0])((([1])(\d{8,9}))|(([2-3])(\d{9})))\Z/
errors.add('Landline number')
end
end
No Responses to “ Regular expression checking of landline/mobile UK numbers in Ruby on Rails ”
Comments:
Leave a Reply
You must be logged in to post a comment.