Itchycontent.com Itchycontent.com Itchycontent.com
  Main Page :> About Us :> Add Your Link :> Privacy of Info :> Terms of Service :> Add Your Article
Search:   
Get Free Links
 

Academics & Learning

Recreation & Entertainment

People & Communities

Computers & Software

Self Help

Garden & Home

Health & Therapy

Teens & Children

Government & Politics

Technology & Science

Games & Play

Banking & Finance

Shopping & Auction

Travel & Accommodation

Property & Agents

Careers & Employment

Business & Services

News & Media

Medical Care

Drink & Food

Automotive

Creative Arts

Fashion & Lifestyle

Sports

 

Main Page –› Computers & Software –› Web Development Services
 

Form Checking - Verifying Name Using PHP Ereg

 
Author: Bernard Peh

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 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.

You can search for this article using: web site development, web design & development, website development tampa
 
 
 

Related Articles

 
From Search to Sale
 
What is Web Hosting and What Do We Get From Cheap Web Hosting
 
Online Advertising Techniques
 
Renew and Update Your Antivirus Software Today
 
PPC Management
 
Team Up With Your Competitors
 
Tips for a Successful Website for Any Organization, NGO or GO
 
Is Click Fraud Really a Problem?
 
3 Steps to Creating Your Own Ezine to Increase Web Site Traffic!
 
Pay Per Click Advertising the Sandbox and SEO
 
 
 
 

Directional Afterburners for Tighter Turns, Vertical Flight and Agility

How much agility are you putting into your UAV designs in your computer game simulations? - Lance Winslow
 

DVD R Dual Layer Discs

The digital versatile disc or DVD has many utilities. Apart from storing movie and audio files, thes ... - Jimmy Sturo
 

Internet Security Software

More and more people are taking advantage of the power of the Internet, as it is the most versatile ... - Jason Gluckman
 
 

Spyware Awareness in Today's Environment

Many people are surprised to find out that they may have a spyware problem. - Mitch Johnson
 

Telecommunications Online

Online Telecommunications schools provide working adults with a convenient way to study for a new ca ... - Michael Bustamante
 

Reliable Low Cost Web Hosting

Acquiring web space at a low cost should not be the only criteria for you to decide whom to go with ... - Alison Cole
 

5 Reasons Why You Are Losing Money If You Don't Podcast

Why should any business podcast? Probably not, unless of course they realize how much they are losin ... - Brandon Hong
 

So That's What Goes On A Home Page!

In the early days of the World Wide Web, the word went around that the thing to do on a home page is ... - Marcia Yudkin
 
 
Main Page :> Privacy of Info :> Terms of Service
© 2008 www.itchycontent.com All Rights Reserved.