Alex Foxleigh
Alex Foxleigh

Reputation: 1974

Regex to allow letters and numbers but not numbers alone

I'm trying to write a regex for javascript which will allow letters and numbers but won't allow numbers alone.

So '12 Test Street' would validate, as would 'test street' but not '12'.

Not that familiar with regexs so I've no idea where to start. I managed to write:

^([A-Za-z\d\s]+[A-Za-z\s])+$

That does work to a point but if a space is then added to the end of the numbers, it will validate again.

Upvotes: 2

Views: 1772

Answers (2)

dgiugg
dgiugg

Reputation: 1334

If I interpret almost strictly (you did not mention whitespace characters) your question I suppose this will work:

^\d*[a-zA-Z][a-zA-Z\d\s]*$

Upvotes: 1

Tomalak
Tomalak

Reputation: 338406

You can solve this easily with a negative look-ahead:

^(?!\d+$)[a-zA-Z\d\s]+$

Note that this allows space-only strings. I'll leave it as a small exercise to change the expression if that's not desired. :)

Upvotes: 8

Related Questions