2

Some days, I try to create a specific regex to validate a simple "Name" field. Yet even today, could not the way I wanted ...

I would like to validate a field as follows:

  • to write any character A-Z and/or a-z
  • to write numbers since she has some character from A-Z and/or a-z.
  • do not allow write only numbers

For example, valid:

  • Only letters: Microsoft Corporation, Asus Technologies, etc...
  • Letters and Numbers: Tom 182, James 007 Bond, etc... (numbers can be any character position: XXXXX 98998 XXXX 87 XXX )

For example, invalid:

  • 098230983 (only number is invalid!)

The failure code REGEX: (^[a-zA-Z\ \']+$)|([0-9]*[a-z\ \']+$)

PS: regex online: http://gskinner.com/RegExr/

Thanks to all.

Vegetus
  • 723
  • 1
  • 8
  • 15
  • 1
    possible duplicate of [regex for alphanumeric, but at least one character](http://stackoverflow.com/questions/1051989/regex-for-alphanumeric-but-at-least-one-character) – Donut Jun 13 '11 at 13:26

1 Answers1

1

You can try with:

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

It allows string to start with digits, then it has to contain a letter and after that letter - letters and digits are possible.

Also a question here: what about whitespace characters ? Because in Microsoft Corporation there is one.

If it should be possible to match them too try with:

^([0-9 ]*[a-zA-Z][a-zA-Z0-9 ]*)$
hsz
  • 148,279
  • 62
  • 259
  • 315
  • Thank you. Your regex is easier, simpler and easier to understand that my regex. I'm using now is ^([0-9 ]*[a-zA-Z][a-zA-Z0-9 ]*)$ – Vegetus Jun 13 '11 at 13:42