1

May I know how to approve only letters a-z, A-Z and digits 0-9 and special character $!., with regular expression in php?

preg_match("/^[a-zA-Z0-9$!\.\,]{1,100}$/", $name);

What goes wrong here?

mario
  • 141,508
  • 20
  • 234
  • 284
davidlee
  • 3,921
  • 13
  • 54
  • 81
  • 1
    [What have you tried?](http://www.whathaveyoutried.com/) – John Conde Jun 13 '12 at 19:39
  • 1
    It's a simple character class, pretty much as you have it. Where's your hurdle? – mario Jun 13 '12 at 19:40
  • possible duplicate of [Help with php regex for limiting allowed characters](http://stackoverflow.com/questions/6181689/help-with-php-regex-for-limiting-allowed-characters) – mario Jun 13 '12 at 19:41
  • preg_match("/^[a-zA-Z0-9$!\.\,]{1,100}$/", $name); what goes wrong here? – davidlee Jun 13 '12 at 19:41
  • @davidlee: No code in comments please. Use the edit link: http://stackoverflow.com/posts/11022137/edit – mario Jun 13 '12 at 19:43
  • * 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 Jun 13 '12 at 19:44

1 Answers1

2

Off of the top of my head

if (preg_match('/^[^a-z\d\$!\.,]$/i', $string)){
     // fail
}
John Conde
  • 212,985
  • 98
  • 444
  • 485
  • 1
    (In case you don't see the difference, davidlee: escape the ``$``.) I don't think the wildcard ``.`` needs to be escaped though. – slackwing Jun 13 '12 at 20:33