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
Regex Validation for Textareas
Moderator: General Moderators
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: Regex Validation for Textareas
htmlentities does not do validation...it is used for escaping (preparing content for use in particular context- in this case html).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
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Code: Select all
preg_match('/[:alnum:\s' . preg_quote($puncuationChars) . ']+/i', $subject);