Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 03-13-2005, 08:19 AM   #1 (permalink)
DogTags
Registered User
 
Join Date: Mar 2005
Posts: 8
DogTags is on a distinguished road
regex for allowed characters in a form field

Hi All

I'm trying to come up with a regex for validating allowed characters in a form field.

The idea is that if any characters other than those in the approved list are entered in the field, then the form will return an error and message.

I don't know regex from a hole in the ground. So, I'm hoping that someone might have some good ideas about this.

Here is the list of approved characters:

$ALLOWED_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz&#!(-)'+/=?:@\""

Yes, the double quote is also allowed and it is escaped near the end.

What would be a good regex to test for these allowed chars?

Many thanks
DogTags is offline   Reply With Quote
Old 03-13-2005, 08:42 AM   #2 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
Something like the following should work:

PHP Code:
<?php
  $input 
'FOO' .  "\07"//FOO plus a BEL 
//  $input = 'FOO';  //uncomment for a valid response.

//  print_r($input);

  
if (preg_match('/[^A-Za-z0-9&#\!\(\)-+\/=\?:@\'\"]/'$input)) {
     echo 
'Error!';
  }
  else {
     echo 
'Input Validated';
  }

?>
Notice the carot in front of A-Z. That negates the statement, so if preg_match is successful that means something other than the allowable characters was matched.

You may also want to look at the Validate pear class. Looks like a consistent interface to validate input.

-r
idx is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Values from Popup Form to Main Form chrislopezz PHP 7 03-28-2005 12:45 PM
Calculated field in Access 2003 form codeboi MS Technologies ( ASP, VB, C#, .NET ) 1 03-03-2005 06:55 AM
EMERGENCY: Dynamic form processing DavH27 PHP 8 10-27-2004 07:52 PM
populate form field virtualGeorge PHP 4 08-23-2004 08:05 AM
How can I count characters in a text field? jimmyoctane PHP 2 10-18-2003 03:38 PM


All times are GMT -8. The time now is 02:27 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting