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()

?

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

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.