-2

I keep seeing regex on phone numbers that allows the number as all one string, and does not make someone enter the hyphen. Using these:

  • /^[-\s]$/
  • /^(-\s)$/
  • /^[-]$/
  • /^(-)$/
  • /^([-]\s)$/

as a regex allows spaces or no hyphen to be typed at all. How do you require a hyphen to be inserted?

EDIT: There shouldn't be downvotes that claim "you shouldn't be forcing someone to use this!". This is the client's requirement, and if you ever had to read phone numbers that were 1234567890 instead of 123-456-7890, that would enable you to see an area code and phone exchange at a glance, I would think anyone would want this.

And not that it should matter to the question in any way because it was kept vague and specific on purpose, but this is for a textbox that will not require any non-NANP numbers. I did that on purpose so we can focus on how to require hyphens, not reinvent the wheel on phone number regexs.

vapcguy
  • 7,097
  • 1
  • 56
  • 52
  • 4
    I'm unclear as to what you're trying to match. If you're trying to match a phone number, requiring hyphens is a bad idea. – zzzzBov Aug 12 '14 at 03:14
  • Unclear? To ensure hyphens are in the number in the right places. I already have a regex that handles NANP numbers. All I want is something that requires the hyphens in the designate places. And this is a client requirement, not my own. – vapcguy Aug 12 '14 at 03:20
  • 1
    @vapcguy What are the designated places? – mpen Aug 12 '14 at 03:21
  • This is a standard NANP number: XXX-XXX-XXXX – vapcguy Aug 12 '14 at 03:23
  • And I don't get why requiring a specific, readable format is such a bad idea for all the upvotes on zzzzBov and downvote on my question. I don't need a whole regex -- just the portion of it that makes a hyphen required. – vapcguy Aug 12 '14 at 03:26
  • @vapcguy, a better solution for validating user input is to [accept any format](http://stackoverflow.com/a/123681/497418) so long as it contains enough valid digits. – zzzzBov Aug 12 '14 at 03:29
  • @vapcguy Restricting user input for certain things can be a bad idea, as you never know what kind of input is valid for a user in a global Internet (just look at names -- why is it up to the web programmer to dictate that you shouldn't have apostrophes or numbers in your name?). That said, the downvote isn't really appropriate for that (it isn't necessarily for this), particularly since it's a client requirement, which you have little control over (but you can try to nudge them away from it!). – ajp15243 Aug 12 '14 at 03:32
  • I might agree with you both, except that this is specifically for a phone number textbox, where they need to enter in this kind of phone number. It won't be holding anything international (outside U.S.), and they've required hyphens for readability. And I had checked that link before posing my question -- all of the regexs allow the number to be XXXXXXXXXX, which the client does not like. – vapcguy Aug 12 '14 at 03:34
  • @vapcguy For phone numbers, I think it's fair to require this then, particularly since it's likely a business reason for requiring US phone numbers. I would make sure to document it in the (however unlikely) case that the code needs to be used for international purposes in the future, however distant. – ajp15243 Aug 12 '14 at 16:56
  • This is for a U.S. agencies list, only. No int'l agencies. If there were to be int'l agencies tracked, we would stand up a new page with different, more custom validation - likely segregated by country. – vapcguy Aug 12 '14 at 21:05
  • possible duplicate of [A comprehensive regex for phone number validation](http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation) – HamZa Sep 02 '14 at 19:17
  • I was actually asking the question to make it so it specifically wasn't a duplicate of that thread. Nothing in there said how to make hyphens required. But I had to supply context or no one would've understood what I had been trying to do and why. – vapcguy Sep 04 '14 at 05:43
  • Closing the question because it's unclear what I'm asking??? It's unclear I just wanted to know how to force hyphens to be required in a regex?! Re-read the title, for crying out loud! Are you guys sure you're really programmers, or have you just become so beyond these novice ideas so as to believe only esoteric ideas are the only ones worthy of being asked on this site and anything not so deserves to be closed? Ridiculous. So sorry that I don't have your years of experience, but I'm sure there are plenty of others like me that could benefit from having simple & the eccentric questions asked. – vapcguy Sep 09 '14 at 01:14

1 Answers1

2

This simple regex will do the job:

^\d{3}-\d{3}-\d{4}$
Toto
  • 89,455
  • 62
  • 89
  • 125