1

I need a regex to see if the $input ONLY contained alphabetic characters or white spaces also need one to check if $numInput ONLY contained numeric characters or white spaces AND one combined so:

$alphabeticOnly = 'abcd adb';
$numericOnly = '1234 567';
$alphabeticNumeric = 'abcd 3232';

So in all of the above examples alphabetic, numeric, whitespace are allowed ONLY NO symbols.

How can I get those 3 diffrent regular expression?

randomKek
  • 1,108
  • 3
  • 18
  • 34
  • See also [Open source RegexBuddy alternatives](http://stackoverflow.com/questions/89718/is-there) and [Online regex testing](http://stackoverflow.com/questions/32282/regex-testing) for some helpful tools, or [RegExp.info](http://regular-expressions.info/) for a nicer tutorial. – mario Jan 19 '12 at 21:38

3 Answers3

2

This should help you

if (!preg_match('/^[\sa-zA-Z]+$/', $alphabeticOnly){
    die('alpha match fail!');
}

if (!preg_match('/^[\s0-9]+$/', $numericOnly){
    die('numeric match fail!');
}

if (!preg_match('/^[\sa-zA-Z0-9]+$/', $alphabeticNumeric){
    die('alphanumeric match fail!');
}
maček
  • 76,434
  • 37
  • 167
  • 198
2

This is pretty basic

/^[a-z\s]+$/i - letter and spaces
/^[\d\s]+$/ - number and spaces
/^[a-z\d\s]+$/i - letter, number and spaces

Just use them in preg_match()

Biotox
  • 1,563
  • 10
  • 15
1

In order to be unicode compatible, you should use:

/^[\pL\s]+$/      // Letters or spaces
/^[\pN\s]+$/      // Numbers or spaces
/^[\pL\pN\s]+$/   // Letters, numbers or spaces
Toto
  • 89,455
  • 62
  • 89
  • 125
  • @MikeVercoelen: To be able to match other characters than ASCII, such as `ç ñ é è â`. Have a look at http://unicode.org/reports/tr18/ – Toto Jan 20 '12 at 16:17