Page 1 of 1

Regular Expressions question

Posted: Wed Jun 07, 2006 1:54 pm
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!

Re: Regular Expressions question

Posted: Wed Jun 07, 2006 2:10 pm
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