VERY IMPORTANT question about validation using php

This is a discussion on "VERY IMPORTANT question about validation using php" within the PHP Forum section. This forum, and the thread "VERY IMPORTANT question about validation using php are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Mar 9th, 2008, 12:23
Junior Member
Join Date: Dec 2007
Location: auckland
Age: 33
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
VERY IMPORTANT question about validation using php

i am using php in order to validate a form where users register. please help me to solve the following validations.

1. name can have spaces. ex= john smith
presently the validation i am using is if( $fname == "" || !eregi("^[a-zA-Z_]+$", $fname) )
i need the syntax which would accept a-zA-Z WITH A SPACE IN BETWEEN NAMES ex= john smith

2. text can have spaces and special characters ex= ref 100/abcd
presently the validation i am using is if( $depositnumber == "" || !eregi("^[a-zA-Z0-9_]+$", $depositnumber) )
i need the syntax which would accept a-zA-Z0-9 WITH A SPACE IN BETWEEN AND SPECIAL CHARACTERS ex= ref 100/abcd

3. spaces in numbers. ex= 123 4567
presently the validation i am using is if( $phonenumber == "" || !eregi("^[0-9]+$", $phonenumber) )
i need the syntax which would accept 0-9 WITH A SPACE IN BETWEEN
ex= 123 4567

4. in case of [a-zA-Z0-9_] if i remove the "_" after 9 will it have a negative impact or is this a syntax due to which i
should i leave the "_" as part of [a-zA-Z0-9_]

5. using stripslashes() function
due to the above validation there is no way a user can enter special characters which could lead to sql injection. inspite of
this should i still use stripslashes to be on the safe side.

please provide the exact syntax for the above validations to works as specified for different scenarios.

thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 9th, 2008, 14:44
Reputable Member
Join Date: Nov 2007
Location: India
Posts: 150
Blog Entries: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Re: VERY IMPORTANT question about validation using php

http://www.sitepoint.com/article/reg...ressions-php/2
The syntax reference given on this page should help you.
and we could have better thread titles next time plz!
Last Blog Entry: Cross browser nuisance (Feb 11th, 2008)

Last edited by RohanShenoy; Mar 9th, 2008 at 14:47.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Mar 9th, 2008, 15:29
masonbarge's Avatar
Highly Reputable Member
Join Date: Jan 2006
Location: Atlanta GA
Posts: 631
Thanks: 0
Thanked 0 Times in 0 Posts
Re: VERY IMPORTANT question about validation using php

I'm not a regex expert by any means and I use PCRE, so caveat emptor.

Quote:
Originally Posted by sudhakararaog View Post
i am using php in order to validate a form where users register. please help me to solve the following validations.

1. name can have spaces. ex= john smith
presently the validation i am using is if( $fname == "" || !eregi("^[a-zA-Z_]+$", $fname) )
i need the syntax which would accept a-zA-Z WITH A SPACE IN BETWEEN NAMES ex= john smith
In PCRE you can use the x modifier to ignore spacing in the entire expression. ("/^[a-zA-Z_]+$/x")

Your description "name can have spaces" is inadequate; you'd be a lot closer to a solution with a more precise description. If you just want to allow spaces anywhere in the string, use the "x" modifier (if POSIX allows it) or just put a space in the brackets: ("/^[a-zA-Z_ ]+$/x")

Quote:
2. text can have spaces and special characters ex= ref 100/abcd
presently the validation i am using is if( $depositnumber == "" || !eregi("^[a-zA-Z0-9_]+$", $depositnumber) )
i need the syntax which would accept a-zA-Z0-9 WITH A SPACE IN BETWEEN AND SPECIAL CHARACTERS ex= ref 100/abcd
I'm having trouble understanding what you want here - maybe somebody else will understand it. What "special characters"? If you're allowing all special characters, why do you need regex? If there are specific characters you want to allow, just put them inside the brackets.

Quote:
3. spaces in numbers. ex= 123 4567
presently the validation i am using is if( $phonenumber == "" || !eregi("^[0-9]+$", $phonenumber) )
i need the syntax which would accept 0-9 WITH A SPACE IN BETWEEN
ex= 123 4567
Actually, I would advise you not to do this at all, but to use three fields for a US-style telephone number.

However, a simple PCRE regex for a ten-digit US telephone number using spaces as the seperator would be
"/^[2-9][0-9]{2} [2-9][0-9]{2} [0-9]{4}$/".
To match a space, just put a space in the expression; it's a character just like any other.

Quote:
4. in case of [a-zA-Z0-9_] if i remove the "_" after 9 will it have a negative impact or is this a syntax due to which i
should i leave the "_" as part of [a-zA-Z0-9_]
If you remove the "_", then the test will fail if there is a "_" in the string tested. The only reason you see it so often is because it is a "word" character, for example, it is allowed in "word only" file names. In short, for your "name" test, you should remove the "_".

Quote:
5. using stripslashes() function
due to the above validation there is no way a user can enter special characters which could lead to sql injection. inspite of
this should i still use stripslashes to be on the safe side.
No, afaik good regex supersedes stripslashes. There may be a concern, however, if you are uploading to a db, that quotation marks are handled correctly.

This is my best effort but again, I'm no expert.

I would advise you to learn a bit more regex before you try to use it. (Of course, someone could fairly say that I should learn a bit more regex before I try to give advice about it.)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

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
How important are backlinks? mybmodel Search Engine Optimization (SEO) 19 Oct 5th, 2008 17:38
question about validation and sql injection sudhakararaog PHP Forum 5 May 21st, 2008 14:22
Why Accessibility is Important for SEO Webnauts Web Page Design 0 Nov 13th, 2005 07:58


All times are GMT. The time now is 03:22.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42