ondrobaco
ondrobaco

Reputation: 737

regex to check the string contains only letter and numbers but not only numbers

I need a help with regex which checks the string contains only letter and numbers but not only numbers

Valid

* letters
* 1wret
* 0123chars
* chars0123
* cha2rs

Invalid

* 1324
* xcvxxc%$#
* xcv123xxc%$#
* _012chars
* _test

Upvotes: 6

Views: 22708

Answers (10)

Sadat
Sadat

Reputation: 3501

([0-9]*[a-zA-Z]+)+

Upvotes: 0

Ties
Ties

Reputation: 5846

this should work ^([a-zA-Z0-9]*)([a-zA-Z]+)([a-zA-Z0-9]*)$ EDIT sry got the * and the + messed up

Upvotes: -2

Amarghosh
Amarghosh

Reputation: 59451

^(?=.*[a-z])[a-zA-Z0-9]+$

  • (?=.*[a-z]) positive lookahead to make sure that there is at least one letter in the string (but doesn't consume any characters - gives up the matched characters as soon as it returns (in this case, as soon as it finds a letter)).
  • [a-zA-Z0-9]+ make sure string contains only alphanumeric characters.
  • ^ and $ are start and end of string delimiters.

Upvotes: 1

pauljwilliams
pauljwilliams

Reputation: 19225

Personally (I hate regex and find them generally to be hard to maintain), I'd do it in two steps.

  1. Is it all alphanumeric?
  2. Is there at least one letter?

Upvotes: 1

polygenelubricants
polygenelubricants

Reputation: 383716

Here are the components of the regex we're going to use:

  • ^ and $ are the beginning and end of the string anchors respectively
  • \d matches a digit
  • [a-zA-Z] matches a letter
  • [a-zA-Z\d] matches a letter or a digit
  • * is "zero-or-more" repetition

With these, we can now compose the regex we need (see on rubular.com):

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

Here's an explanation of the pattern:

from the beginning...  till the end
|                      |
^\d*[a-zA-Z][a-zA-Z\d]*$
 \_/\______/\_________/

The 3 parts are:

  • Maybe some digits as a prefix...
  • But then definitely a letter!
  • And then maybe some digits and letters as a suffix

References

Upvotes: 35

Carl
Carl

Reputation: 7544

^[0-9]*[a-zA-Z]+[0-9a-zA-Z]*$

translated: from the beginning, match 0 or more numbers, then match at least one letter, then match zero or more letters or numbers until the end.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655189

This should do it:

^[0-9]*[a-zA-Z]+[a-zA-Z0-9]*$

This requires at least one character of [a-zA-Z].

Upvotes: 6

Piotr Müller
Piotr Müller

Reputation: 5548

^([a-zA-Z0-9]*)([a-zA-Z]+)([a-zA-Z0-9]*)$

Upvotes: 1

BoltClock
BoltClock

Reputation: 723468

Instead of using a regular expression, you can also use the ctype_*() functions:

var_dump(ctype_alnum('letters') && !ctype_digit('letters'));     // bool(true)
var_dump(ctype_alnum('0123chars') && !ctype_digit('0123chars')); // bool(true)
var_dump(ctype_alnum('1324') && !ctype_digit('1324'));           // bool(false)
var_dump(ctype_alnum('xcvxxc%$#') && !ctype_digit('xcvxxc%$#')); // bool(false)

But if you want a regular expression, you can use this:

var_dump(preg_match('/^[a-z0-9]*[a-z]+[a-z0-9]*$/i', $input));

Upvotes: 2

Vitalii Fedorenko
Vitalii Fedorenko

Reputation: 114420

[a-z0-9]*[a-z]+[a-z0-9]*

Upvotes: 2

Related Questions