Prasad Raja
Prasad Raja

Reputation: 703

Regular expression "Only text should allow character or Text and number and should accept"

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

Answers (3)

Wasiq Muhammad
Wasiq Muhammad

Reputation: 3138

Here it works for you

var regex =  /[a-zA-Z0-9]*[a-zA-Z][a-zA-Z0-9]*/g

Upvotes: -1

Lucas Trzesniewski
Lucas Trzesniewski

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

ndnenkov
ndnenkov

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

Related Questions