Vishal Singh
Vishal Singh

Reputation: 628

Regular Expression: Allow letters, numbers, and spaces (with at least one letter not number)

I'm currently using this regex (/^[A-Za-z0-9 _]*[A-Za-z0-9][A-Za-z0-9 _]*$/) to accept letters, numbers, spaces and underscores. I want to change it like this that it takes the combination of number and the character but not only the number.

Upvotes: 4

Views: 16336

Answers (4)

Brian Min
Brian Min

Reputation: 297

/^[\w\s]+$/

\w allows letters, numbers and underscores

\s allow spaces

Upvotes: 2

Wiki
Wiki

Reputation: 43

Try this:
^(?![0-9]*$)[a-zA-Z0-9\s_]+$

This expression has a negative lookahead to verify that the string is not only numbers. See it in action with regexr

Upvotes: 1

gdyrrahitis
gdyrrahitis

Reputation: 5978

If i understand correctly you want to allow a string that begins with at least one letter and optionally is followed by number or underscore or space.

Try this: /^(?:[A-Za-z]+)(?:[A-Za-z0-9 _]*)$/ at this online regex tester.

This should work.

Cheers!

Upvotes: 8

sideroxylon
sideroxylon

Reputation: 4416

Try this:

/^[A-Za-z0-9 _]*[A-Za-z]+[A-Za-z0-9 _]*$/

This allows for 0 or more of each of the outside groups, and 1 or more of the inner group (letters only). A string of only digits will fail.

Upvotes: 6

Related Questions