Regex Validation for Textareas

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

Moderator: General Moderators

Post Reply
1Copenut
Forum Newbie
Posts: 5
Joined: Mon Jun 19, 2006 1:48 pm

Regex Validation for Textareas

Post by 1Copenut »

Hello:

I'm writing a simple contact script that emails my customer's name, email and message. I'm wanting to use regular expressions to validate their input in the textarea, and am not sure how to write the expression. I want the user to be able to use all letters, numbers and basic punctuation, but nothing else.

I've used basic htmlentities validation in the past, but know there is something better with regexes.

Thank you,

Trevor
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Moved :arrow: to regex.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Re: Regex Validation for Textareas

Post by jmut »

1Copenut wrote:Hello:

I'm writing a simple contact script that emails my customer's name, email and message. I'm wanting to use regular expressions to validate their input in the textarea, and am not sure how to write the expression. I want the user to be able to use all letters, numbers and basic punctuation, but nothing else.

I've used basic htmlentities validation in the past, but know there is something better with regexes.

Thank you,

Trevor
htmlentities does not do validation...it is used for escaping (preparing content for use in particular context- in this case html).

You really need regexp.
What do you consider "basic punctuation"?
Can you give us example of names you want to pass and some you don't.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Have you ever used regex before? If so, have you taken a stab at this one yet?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

preg_match('/[:alnum:\s' . preg_quote($puncuationChars) . ']+/i', $subject);
Post Reply