/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Validation in php

Php validation

if(!preg_match(‘^[a-zA-Z]+$’,$_POST[‘username’]))

I want to make it that $_POST[‘username’] can [B]only[/B] consist of letters A-Za-z, however, don’t know how. What I am trying to make is an if() statment that checks if the string $_POST[‘username’] does not only consist of letters hence the use of the ! before the preg_match statement.

I don’t know how to do this, I am very new to php and would like some help, this error message is coming up:

Warning: preg_match() [function.preg-match]: No ending delimiter ‘^ found in

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 18.2010 — [code=php]if(!preg_match('/^[a-zA-Z]+$/',$_POST['username'])) {print "invalid username";} else {print "valid username";}[/code]

If you wish to allow a space in the username as well, just add s after the upper case Z.

-jim
Copy linkTweet thisAlerts:
@esquiladoauthorFeb 18.2010 — Thanks, If I allow a space, will the user be allowed to [I]only[/I] use spaces, with no text, I wouldn't mind allowing a space in the middle of the name, but I don't want to only allow spaces.
Copy linkTweet thisAlerts:
@esquiladoauthorFeb 18.2010 — And I don't want them to be able to end the name with a space.
Copy linkTweet thisAlerts:
@NogDogFeb 18.2010 — You could first trim() the value to remove leading and trailing spaces.
Copy linkTweet thisAlerts:
@esquiladoauthorFeb 18.2010 — Oh ok, thanks.
Copy linkTweet thisAlerts:
@esquiladoauthorFeb 18.2010 — How do I count how many characters are in a string? Because I could use trim(), and then after that do a check that the string has got at least two characters.
Copy linkTweet thisAlerts:
@criterion9Feb 18.2010 — strlen($string) gives you the length of a string.
Copy linkTweet thisAlerts:
@esquiladoauthorFeb 18.2010 — Thanks =)
Copy linkTweet thisAlerts:
@iandevlinFeb 18.2010 — thestring.length
Copy linkTweet thisAlerts:
@criterion9Feb 18.2010 — thestring.length[/QUOTE]

That is java/javascript syntax. It won't work in PHP.
Copy linkTweet thisAlerts:
@iandevlinFeb 18.2010 — That is java/javascript syntax. It won't work in PHP.[/QUOTE]

Ah crap, I thought he was on about JavaScript for some reason. My mistake!
Copy linkTweet thisAlerts:
@criterion9Feb 18.2010 — Lol. I do that sometimes too. I just wanted to make sure to clarify so anyone following the thread wouldn't be confused.
Copy linkTweet thisAlerts:
@iandevlinFeb 18.2010 — Lol. I do that sometimes too. I just wanted to make sure to clarify so anyone following the thread wouldn't be confused.[/QUOTE]

Sure, no worries! ?
Copy linkTweet thisAlerts:
@esquiladoauthorFeb 18.2010 — I want to learn javascript, so I'll keep a mental note of that =)
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 18.2010 — Man, this thread got busy fast.

Just for a second, going back to the original issue:

[code=php]$_POST['username']=preg_replace("/[s]{2,}/"," ",trim($_POST['username']));[/code]

When the above code is added [U]after[/U] the expression I gave you earlier, after all is said and done, a username can only be alphabetical, no spaces on the front or the end, and [COLOR=Red]no more than 1 space in the middle[/COLOR] if spaces are found. ?

If someone feels like merging it all into one expression, go for it, but the user has something to work with now if they end up permitting a space in a username.

-jim
Copy linkTweet thisAlerts:
@esquiladoauthorFeb 18.2010 — [code=php]if(!preg_match('/^[a-zA-Z]+$/',$_POST['username']))

{
echo "Only letters can be entered into this field, no spaces, special characters or numbers are permitted. Please try again.";
}
elseif((mysql_num_rows(mysql_query("SELECT ipadd FROM members WHERE ipadd = '$ipadd'")))==0)
{
$trimUname = trim($_POST['username']);
if(strlen($trimUname < 2))
{
echo "Your name must consist of at least two characters.";
}
else
{
$dUsername = @$_POST['username'];
$timenow = time();
mysql_query("INSERT INTO members (ipadd,username,timeJoined) VALUES ('$ipadd','$dUsername','$timenow')");
echo "<br>You have now been added to the database.";
}
}[/code]


Thank you, so where would I put that in this code?
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 18.2010 — If you opt to allow a space after all (hey, it was just a suggestion above, it's not required) I'd personally rework that entire section. I prefer to do all the validation/formatting once in a function to centralize the code (see code comments below), then do all the query stuff after.

That's just me.

Example (not your code, just to demonstrate the concept, tested/works):

[code=php]$username=ValidateUserName($_POST['username']);
if (!$username) {print "Only alphabetical case insensitive characters and an optional single space are allowed!";}
else {
//... rest of your code ...
print "Welcome, $username...";
}

function ValidateUserName($myusername) {

$myusername=preg_replace("/[s]{2,}/"," ",trim($myusername));
if(!preg_match('/^[a-zA-Zs]+$/',$myusername)) {
return false;
}

/* If necessary in the future add more tests here if username
policy changes. Just return false if any test fails.
Otherwise, username is valid, let's return that value. */

return $myusername;
}
[/code]


To each their own. I've done all I can do here, gotta scoot. Cya! ?

-jim
Copy linkTweet thisAlerts:
@SrWebDeveloperFeb 18.2010 — Shoot, forgot to clarify, I meant no more than one [U]consecutive[/U] space allowed. This allows usernames like "John R Brown". If you want to limit it to one space total, just throw in another condition in my function that counts how many spaces in the string and return false if >1 and re-word the invalid username message I display to the user, duh. >poof< I'm gone!

-jim
Copy linkTweet thisAlerts:
@esquiladoauthorFeb 19.2010 — Thank you ever so much!! I did the one above, where it disallows consecutive spaces, but didn't do the second one =).

[B]Thank you!!!![/B]
×

Success!

Help @esquilado 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.25,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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