Reputation: 703
I need a regular expression for a text field.
The requirement is:
The text box should allow only combinations of digits and letters (but not digits alone)
Accept only letters:
^[a-zA-Z]+$
Accept only digits and letters:
^[a-zA-Z0-9]+$
was trying to merge both in one condition..
Upvotes: 0
Views: 122
Reputation: 3138
Here it works for you
var regex = /[a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*/g
Upvotes: -1
Reputation: 51430
So you need at least one letter:
^[a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*$
Or you can use a lookahead:
(?=.*?[a-zA-Z])^[a-zA-Z0-9]+$
Be aware that these solutions are limited to English letters only - if you need international letter support, you could use the XRegExp library, along with the following pattern:
(?=.*?\p{L})^[\p{L}\d]+$
Upvotes: 2
Reputation: 36110
^[a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*$
The idea is - if you put a letter somewhere, there would definitely be at least one letter.
Upvotes: 2