jayjay960 Posted July 20, 2009 Share Posted July 20, 2009 Hi, this is an unfinished script of mine for registering user accounts: <?php $username = $_GET['username']; $password = $_GET['password']; $confirmpassword = $_GET['confirmpassword']; $email = $_GET['email']; $birthday = $_GET['birthday']; $birthmonth = $_GET['birthmonth']; $birthyear = $_GET['birthyear']; $pass = true; $passmessage = ''; if (strlen($username)<3 || strlen($username)>20) { $pass = false; $passmessage = 'Invalid username. Username must be between 3-20 characters long.'; } if ($pass && preg_match('[^a-zA-Z0-9_]', $username)) { $pass = false; $passmessage = 'Invalid username. Username can only contain characters a-z, 0-9 or _.'; } echo $passmessage; ?> It's actually just a page called via ajax and the results are dumped in a box on the registration page. At the moment I've just started to get it to validate the user details, but I've come across a problem in this particular if statement: if ($pass && preg_match('[^a-zA-Z0-9_]', $username)) What I want to do is check if $username contains a character other than a-z, A-Z, 0-9 or _, but it's returning false every time. I've always had problems with regular expressions, and it's annoying me, because I've been using this reference and I can't see what I'm doing wrong. Can anyone help? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 you need terminators at the beginning and end if ($pass && preg_match('/[^a-zA-Z0-9_]/', $username)) you can also shorten it up with: if ($pass && preg_match('/\\W/', $username)) Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 20, 2009 Share Posted July 20, 2009 A few things to keep in mind... when something like [^a-zA-Z0-9_] is successful (in that, in this case since we are dealing with a negated character class, if the value contained within $username doesn't have any characters outside of a-zA-Z0-9_), that preg statement will return 0: example: $username = 'thYui32_i9u'; echo preg_match('#[^a-zA-Z0-9_]#', $username); // output: 0 Since this is being checked in an if statement in conjunction with the variable $pass (which is defaulted to true - and let's assume that the strlen is not an issue, in that the username passes this as well), this means that in a valid value for $username, the preg statement will return 0 (false), yet $pass = true.. as a result, the entire if statement fails... I think I would tackle it this way: Example based off original code: $username = 'thYui32_i9u.'; $pass = true; if ($pass && preg_match('#^[a-z0-9_]+$#i', $username)){ echo $username; } else { $pass = false; echo $passmessage = 'Invalid username. Username can only contain characters a-z, 0-9 or _.'; } @rhodesa, I'm not sure if that \\W is a mistype, but perhaps you meant simply \W? The thing to be careful of when dealing with shorthand character classes like \w or even \W is that depending on the locale, \w might not be only [a-zA-Z0-9_] (my locale allows a-zA-Z0-9_, exponents and accented characters as well) and as a result, this could botch up \W as well. If using those kinds of shorthands, I would first set the ctype setting to nullify these possible issues: Insert this prior to any regex: setlocale(LC_CTYPE, 'C'); Quote Link to comment Share on other sites More sharing options...
jayjay960 Posted July 21, 2009 Author Share Posted July 21, 2009 Thanks everyone. Actually I solved this late last night after a lot of looking around. I worked out I needed terminators like rhodesa said. Also I seem to be getting the hang of regular expressions now, I worked out one for making sure email addresses follow the format abc@def.ghi. If anyone's interested here's the username one: <?php preg_match('/^[a-zA-Z0-9_]+$/', $username); ?> The email one: <?php preg_match('/^.+@.+\..+$/',$email); ?> And another script I came up with to check if a date is valid (assuming the user has been given the options 1-31 for the date and 1-12 for the month. $birthday is the date, $birthmonth is the month and $birthyear is the year. at the end of the script $pass will be true if the date is valid or false if otherwise.) <?php $pass = false; switch($birthmonth) { case '2': { if (round($birthyear/4)==$birthyear/4) { if ($birthday>29) { $pass = false; } } else { if ($birthday>28) { $pass = false; } } break; } case '4': { if ($birthday>30) { $pass = false; } break; } case '6': { if ($birthday>30) { $pass = false; } break; } case '9': { if ($birthday>30) { $pass = false; } break; } case '11': { if ($birthday>30) { $pass = false; } break; } } ?> Quote Link to comment Share on other sites More sharing options...
.josh Posted July 21, 2009 Share Posted July 21, 2009 I worked out one for making sure email addresses follow the format abc@def.ghi. The email one: <?php preg_match('/^.+@.+\..+$/',$email); ?> sweet! where do I sign up? I just got this new email address: 1@1.1' RAPE YOUR DATABASE CODE HERE Quote Link to comment Share on other sites More sharing options...
jayjay960 Posted July 22, 2009 Author Share Posted July 22, 2009 Ah yes I forgot about that.. Thanks, I'll fix that hole up. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.