954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Regular expression match

Hi all
i want to check regular expression of a text field value
i.e.
it should not contain any '.,@' and other special characters except '_' and '-'.
How can i write my rule in

preg_match()


?

baig772
Junior Poster
123 posts since Mar 2011
Reputation Points: 29
Solved Threads: 6
 

Hi,
I think this link will help you.
http://php.net/manual/en/function.preg-match.php

subrata_ushasi
Junior Poster in Training
52 posts since Aug 2008
Reputation Points: 10
Solved Threads: 5
 

Dear subrata
i have already read this documentation.
still i am unable to write my own rule
:(

baig772
Junior Poster
123 posts since Mar 2011
Reputation Points: 29
Solved Threads: 6
 

So only a-z, 0-9, - and _

if (preg_match('/[\w-]*/', $subject)) {
  # Successful match
} 
else {
  # Match attempt failed
}

Update: As minitauros states, use a + instead of a * if you want to match at least a single character.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

Here's a good tutorial on regular expressions:

http://www.phpro.org/tutorials/Introduction-to-PHP-Regex.html

In your case, I think you're looking for something like

preg_match('/^[0-9A-Za-z_\-]+$/', .....)

Which returns true if the input string contains nothing more than letters, numbers, underscores and dashes, or indeed, like pritaeas says

preg_match('/^[\w-]+$/', .....)


which is similair to that.

minitauros
Junior Poster
109 posts since Apr 2011
Reputation Points: 36
Solved Threads: 17
 
baig772
Junior Poster
123 posts since Mar 2011
Reputation Points: 29
Solved Threads: 6
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: