-3

What would be regex for a phone number of this format +91-9999999999?

Format: +(country code)-(10 digit phone number)

I found many questions similar to this such as: Regex for phone number with country code

But they are not specific for this format.

Also if possible could you tell me how to use it in ruby if possible or I will figure it out myself.

hsvahkj
  • 41
  • 7
  • 1. Could you be a little more especific about the condition for your regex, I'm understanding that the first number can be any from 1 to 9, the second from 0 to 1 and the others also from 0 to also, am I right? In that case you can use \+[1-9][0-1]-[1-9]{1}[1-9]{9} – Abdel P. Aug 27 '20 at 04:39
  • @AbdelP. Format is like this: `+(country code)-(10 digit phone number)` – hsvahkj Aug 27 '20 at 04:42
  • @AbdelP. I think we can take it like this: `+(some numbers)-(some numbers)` – hsvahkj Aug 27 '20 at 04:43
  • Have you checked this answer already? https://www.google.com/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/2113908/what-regular-expression-will-match-valid-international-phone-numbers&ved=2ahUKEwjCpvrgybrrAhWjH7kGHWmOBrkQjjgwAHoECAoQAQ&usg=AOvVaw3gqetYG34tYejWbL2TJbjU – Abdel P. Aug 27 '20 at 04:48
  • Maybe is the first or the second answer what you need – Abdel P. Aug 27 '20 at 04:48
  • What is the code you are having trouble with? What trouble do you have with your code? Do you get an error message? What is the error message? Is the result you are getting not the result you are expecting? What result do you expect and why, what is the result you are getting and how do the two differ? Is the behavior you are observing not the desired behavior? What is the desired behavior and why, what is the observed behavior, and in what way do they differ? Please, provide a [mre]. Please be aware that [so] is not a code-writing service, you need to show your efforts! – Jörg W Mittag Aug 27 '20 at 05:31

1 Answers1

1

Please don't validate user input this way. If you're trying to validate user input, it's generally better to verify that you have the right number of digits and then do your own formatting. To validate that you have 11-13 digits in your input string, even if it's not perfectly formatted, consider the following:

input = " + ( 11 ) 123-456-7890 "
input.delete! "^0-9"
input.length >= 11 && input.length <= 13
#=> true

input_chars = input.chars
phone_number, country_code = input_chars.pop(10).join, input_chars.join
p formatted_number = "+(%d) %d" % [country_code, phone_number]
#=> "+(11) 1234567890"
  

On the other hand, if you have a data set where you want to validate the formatting itself, that's a different story. You can look for 1-3 digits followed by 10 digits, along with whatever anchors or formatting you're looking for. For example:

expected_format = /\A\+\d{1,3}-\d{10}\z/

input =  "+91-9999999999"
input.match? expected_format
#=> true

input =  "91-9999999999"
input.match? expected_format
#=> false

input = "+1111-012345689"
input.match? expected_format
#=> false
Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199