Use regex.
^\d[-0-9]+\d$
It is assumed that the string length is at least three characters.
Structure:
^ - match start of string \d - match a digit [ - start of character class containing: - - a dash 0-9 - 0 to 9 ] - end of character class + - match one or more of the previous \d - match a digit $ - match end of string
You can change + to * to make 2-digit strings valid, and add striping to make 1-digit strings also:
^(\d|\d[-0-9]*\d)$
Note. In .NET, \d will match any Unicode digit (for example, Arabic digits will match) - if you don't want this, replace \d with [0-9] in every place.
Odded
source share