• Post Reply Bookmark Topic Watch Topic
  • New Topic

String only contains digits and "-"  RSS feed

 
Gregers Svensson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i make a method that makes sure that a String only contains digits and "-" (dash) ... (I want to make sure that the method can see if the user has written in an okay telephone number...)


This is what I've done this far.. But the method retuns false if you type - or +



Help much appreciated! Thanks
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
generally I use regex to do things like these. It allows you to not only check if the string is made up of numbers and dashes, but also check the string os of the format NNN-NNN-NNNN

If you don't want to use regex, you can split the string by "-" and then check each part to see if each is a number
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want something more specific than "only digits and -". For example you might not like "1-2---3---" or many other such strings. Probably you only want phone numbers correctly formatted for your country? If that's the case then I think a regex is the right way to go.
 
Shashank Gollapudi
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use regex of the format \d{3}-?\d{3}-?\d{4} where \d represents a numeric digit and the number in the curly braces indicate the number of digits that are possible.
for example \d{3} should contain three digits.
? (this indicates the character preceding can be ignored or can be used once). In our case -(dash) can be either ignored or can be used once.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^(\d+-?)*\d+$
This will match any String which contains only digits and dashes but does not allow two dashes in a row and doesn't allow dash at the beginning and/or end of the String.

^\+?(\d+-?)*\d+$
The same as above, but also allows + sign at the beginning.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregers Svensson wrote:I want to make sure that the method can see if the user has written in an okay telephone number...

Which is not as easy as it might seem.

The first thing is that telephone numbers in different countries have different formats, so you'd need to be very clear about what country you're dealing with.

The second is that dashes are a visual way for us puny humans to break up a telephone number; they are NOT part of the number itself. An alternative (as I believe someone else already suggested) would be to split the number itself into distinct fields without dashes, eg: country code, area code/exchange, line number.
Then you would only have to deal with actual numbers.

There is a lot to know about telephone numbers, as there is about any standardised number or code, and the one for telephone numbers is E.164 - which, as you can see, is not small.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!