myarticlecenter.com myarticlecenter.com
Main >> About Us >> Place Your Link >> Privacy of Info >> Terms of Use >> Submit Article
Search:   
 
 

Surefire Google Adsense Earnings Tips

How you can create additional website income by creatively using Google adsense ads. - Elizabeth McGee
 

Eight Things To Ponder Upon

A few points for the less-experienced e-marketers to consider When evaluating their business plan. - Homer Farey
 

7 Golden Rules For Building Mini Sites

How would you like to learn 7 eye opening Golden Tips that makes the great affiliate earners so much ... - Janis Searcey
 

Why the Need to Renew School Mission Today?

High-tech mutations have posed -and continue to posing- serious challenges to school, which had to i ... - Gamra Zenaidi
 

FREE Advertising For Your Internet Business

This article contains information on how to advertise your internet business for free. - Steve Gitto
 

Mobile Phone Contracts: Look Before You Leap!

Mobile phones are a great communication tool, but the contracts for mobile phone plans are binding a ... - John Bertone
 
 

  Main » Software & Networking » Web Design & Development
   
 

Form Checking - Verifying Name Using PHP Ereg

   

One important use of Regular Expressions (Regex) is to verify fields submitted via a form. In this article, we attempt to write an expression that is able to verify the user's first name, middle name, last name or just names in general.

The expression should allow names such as "Mary", "Mr. James Smith" and "Mrs O'Shea" for example. So the challenge here is to allow spaces, periods and single quotation marks in the name field and reject any other characters.

Elimination Technique

We try to identify and detect all illegal characters in the name field. I came up with the following list:

Punctuations: ~`!@#$%^&*()=+{}|:;<>"/?,

Numerics: 0-9

Noticed that I left out the empty space ( ), period (.) and single quotation mark (') because we are allowing these 3 characters to pass the verification. In other words, the verification will fail if the name field contains any of the punctuations or numerics above.

The Regex

Now, the hardcore part. The regex pattern I came up with is as follows:

([[:digit:]]|[~`!@#$%^&*()_=+{}|:;<>"/?,]|[|]|-)+

Let me briefly explain what this pattern means. The expression can be represented by:

(expression1 | expression2 | expression3 | expression4 | expression5)

What we are trying to do here is to match the name field to the patterns in expression 1, 2, 3, 4 or 5. If you look at the regex closely, you will see that expression1 is actually [[:digit:]].

Expression2 is:

[~`!@#$%^&*()_=+{}|:;<>"/?,]

Noticed that I added a backslash () before each of the 5 characters "()+|". By backslashing these characters, I am telling the function to treat the characters as it is and not as special built-in characters. For example, the brackets "()" actually means grouping in regex but if I backslash it, ie "()", it simply means that I want to match "(" and ")".

Expression3 is "[", expression 4 is "]" and expression 5 is "-". We left out the 3 characters "[]-" in expression2 just to avoid confusion because we already used "[]" as the outer brackets. As for "-", we left it out because it is normally used as a range within the brackets "[]", like so [A-Z].

Implementation

To implement it in PHP, we write the code as follows:

$pattern = '([[:digit:]]|[~`!@#$%^&*()_=+{}|:;<>"/?,]|[|]|-)+';
$name = stripslashes({$_POST['name_field']});
if (ereg($pattern,{$_POST['name_field']})) {
echo "write your error message";
}

We stripslashed the name field just in case your have magic quotes turned on. If magic quotes is turned on, the single quotation mark will be passed as ' instead just '. The ereg function will look for digits and illegal punctuations in the $_POST name field. If an error is found, we can do something such as alerting the user of the error.

Conclusion

Hopefully, this article can give you some insight into regex and save you some time when verifying name fields. You can modify the regex to have stricter rules for example, you may not want the name field to start with a space or a period. That's all for now. Cheers.

Author: Bernard Peh
 
Author Bio:

Bernard Peh

Bernard Peh is a great passioner of web technologies and one of the co-founders of Sitecritic.net Site Reviews. He works with experienced web designers and developers for more than 5 years, developing and designing commercial and non-commercial websites. During his free time, he does website reviews, freelance SEO and PHP work.

 
 
 

Related Articles

 
The 'Quick & Easy' Money Makers!
 
Keep Your Navigation Simple!
 
How to remove Spyware
 
How to Become an Expert in Your Niche Business
 
Computer Training - Networking
 
The High Cost of Using a Cheap Template Website
 
8 Tips On Buying The Mobile Phone That??s Right For You!
 
SEO and Your Targeted Market
 
Avoid these Strategies to Keep Your Website Visitors Happy and Make Money
 
How To Submit Articles Effectively For SEO
 
 
 
Multiple links exchange
 

Home & Garden

Education & Learning

Companies & Business

People & Society

Software & Networking

Fashion & Relationships

Healthcare & Treatment

Government & Politics

Children & Teens

Tour & Travel

News & Events

Recreation

Jobs & Employment

Science & Space

Estate & Realty

Art & Creative

Finance & Investment

Malls & Shopping

Self Enhancement

Sports

Online & Board Games

Drink & Food

Fitness & Health

Automobiles

 
Main >> Privacy of Info >> Terms of Use  
Copyright © 2006-2008 www.myarticlecenter.com - All Rights Reserved.