2

How can I validate that a string ends with exactly three digits?

valid:

Hey-12-Therexx-111
001
xxx-5x444

invalid:

Hey-12-Therexx-1111
Hey-12-Therexx-11
Hey-12-Therexx
12
112x
0

1 Answer 1

9

You can write:

/(^|\D)\d{3}$/

which means, "start-of-string-or-non-digit-character, followed by three digits, followed by end-of-string".

Sign up to request clarification or add additional context in comments.

7 Comments

Your rule restrict the input to 3-4 characters. But the input seems to be longer...
@rekire that is incorrect. He has the correct expression. Try it yourself and see.
inhan, that doesn't work. 3x3333 is invalid, but yours lets it through.
@ruakh Why the edit? You modified the question so it isn't necessarily asking the same question.
@MichaelMorgan: What do you mean, it isn't "necessarily" asking the same question? Either you think it is, in which case, no problem; or you think it isn't, in which case, please be more specific: what do you think has changed about it?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.