Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-20-2011, 09:56 AM regexp question
Junior Talker

Posts: 1
Trades: 0
I'm trying to do some form validation in php using preg_match. This is what I would like to allow:

Any letters from a-z, regardless of case
can include the characters . - ' ,
can include spaces
all of the above should be allowed to be in any order throughout the string.

I do not want it to include numbers, semi-colons, colons, hash (pound sign), back or forward slashes, brackets (either curly or square), dollar signs, greater than or less than signs, or other characters such as that.

I have written:

Code:if (!preg_match("%[a-zA-Z\.\,\-\'\ ]%",$field_name))
{
invalid output would go here
exit;
}


It must be wrong because I am allowed to enter things like ...
O'Sm#ith
Smith 1
Smith1

I haven't the foggiest I idea how to correct my problem. I've had a look at some regexp sites to try to debug my syntax, but that this point I'm lost. Can anyone give me some hints?

Cheers
Heiko

Last edited by Tinkli; 01-20-2011 at 11:00 PM..
Tinkli is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-20-2011, 10:41 AM Re: regexp question
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I think this expression should do it:
Code:
/^([a-zA-Z]|[\'\,\.\-\s])*$
PHP Code:
$field_name = array('O\'Sm#ith''Smith 1''Smith1''Smith''O\'Smith''Sm-ith');

foreach(
$field_name as $field)
{
    echo (int) 
preg_match("/^([a-zA-Z]|[\'\,\.\-\s])*$/"$field);
}

//output: 000111 
I never was particularly good with regular expressions so there is a good chance someone will come along with a better solution.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to regexp question
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27466 seconds with 12 queries