/    Sign up×
Community /Pin to ProfileBookmark

Form validation for name and address?

Hi, I am in the process of securing my order form but I don’t know what to do for the first name, last name, address line 1 & line 2 and city. All the fields like zip code, email, phone number have very strong regular expressions to validate the input, prevent unintentional errors and provide protection against sql injection. SO my question is how should I validate first name, last name, address line 1 & line 2 and city. I know I won’t be able to fully protect these fields against errors but I at least need protection from sql injection. Should I just use mysql_real_escape_string() on these fields? but what if theres a backslash in the input that is supposed to be there but is removed when I call strip slashes? Thanks

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@tirnaApr 29.2010 — For strings like names, addresses, phone numbers I have a list of valid characters for each string type and then check each character in the string to make sure it is a valid character. I do this on both server and client side.
Copy linkTweet thisAlerts:
@NogDogApr 29.2010 — I tend to assume that the user knows how to spell his name and address, and prefer not to try to guess what all the possible valid characters should be, as if I guess wrong, I may make it difficult/impossible for a user to enter valid data. I'll typically trim() the value, check that it is within the min/max allowed length, and then maybe check for obviously illegal values such as newlines and carriage returns and nulls.
[code=php]
if(preg_match('/[x00-x31]/', $string))
{
// invalid character
}
[/code]
×

Success!

Help @Rob4226 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...