I want to create a regular expression in such a way that only hypens and digits must be allowed in the text box the criteria is
Thanks in advance
Here's a shortened version of @El Yobo's regex. You can replace [0-9]
with \d
and you can make the hyphen optional with -?
to remove the special case of hyphenless strings.
^\d+(-?\d+)*$
http://ideone.com/SRqPW
This regular expression should do it:
^[0-9]+(-[0-9]+)*$
This will match one or more digits, that may be followed by zero or more sequences of a hyphen followed by one or more digits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With