Regular Expressions question

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
latinomigs
Forum Newbie
Posts: 2
Joined: Wed Jun 07, 2006 1:48 pm

Regular Expressions question

Post by latinomigs »

I'm trying to validate a form input object of type text using regular expressions.

I would like to allow any number or combination of letters, numbers, and/or the characters "_","-", "&".

Anything else I'd like to have recognized as invalid.

Any ideas of how to do this with regular expressions in php?

Thanks!
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Regular Expressions question

Post by aerodromoi »

latinomigs wrote:I'm trying to validate a form input object of type text using regular expressions.

I would like to allow any number or combination of letters, numbers, and/or the characters "_","-", "&".

Anything else I'd like to have recognized as invalid.

Any ideas of how to do this with regular expressions in php?

Thanks!
This should do the trick:

Code: Select all

<?php
$string = "test01_-:abc/1&";
if(!eregi("^([a-zA-Z0-9\&_\-]*)$", $string )) {
  echo "invalid!";
}
else{
  echo "success";
}
?>
aerodromoi
Post Reply