Far Cry Posted April 16, 2012 Share Posted April 16, 2012 I am writing a validation script that validates the inputted username and password, although not totally finished. I just decided to test it and see if it was working as it should. I get these errors when running this file: Warning: preg_match() [function.preg-match]: Compilation failed: nothing to repeat at offset 5 in C:\*****\*********\********\includes\validation.php on line 21 Warning: Invalid argument supplied for foreach() in C:\****\******\******\includes\validation.php on line 75 What this is supposed to do hold all errors in an array, and then echo them out to the user. And the valid property is self-explanatory. <?php class RegisterValidation { public $error = array(); public $valid; public function ValidateUsername($username) { /* X First sanatize username. X Then check length. Then chick if username exists in DB or not. */ trim($username); strip_tags($username); mysql_real_escape_string($username); if(!preg_match('/{3,20}/', $username)){ $this->valid = false; $this->error = "Your username must be 3 to 20 characters in length."; } else { $this->valid = true; } return $this->valid; } public function ValidatePassword($password,$repassword) { /* First sanatize. Then check if pass has at least one upper, lower, and one number. */ trim($username); strip_tags($username); mysql_real_escape_string($username); if(!preg_match('/[A-Z]/', $password)) { $this->valid = false; $this->error = "Your password must contain at least one upper-case letter."; } else { $this->valid = true; } if(!preg_match('/[a-z]/',$password)) { $this->valid = false; $this->error = "Your password must contain at least one lower-case letter."; } else { $this->valid = true; } if(!preg_match('/[0-9]/')) { $this->valid = false; $this->error = "Your password must contain at least one number."; } else { $this->valid = true; return $this->valid; } } } $register = new RegisterValidation; $usertest = "uyrewuiy"; $register->ValidateUsername($usertest); foreach ($register->error as $error){ echo $error; } ?> Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/261069-php-foreach-classes-regex/ Share on other sites More sharing options...
RickXu Posted April 16, 2012 Share Posted April 16, 2012 if(!preg_match('/{3,20}/', $username)) As the error suggested, this line is not right. You may consider checking the preg_match and regular expression. I'd write the regex as: /^[\w]{3,12}$/im Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/261069-php-foreach-classes-regex/#findComment-1337962 Share on other sites More sharing options...
kicken Posted April 16, 2012 Share Posted April 16, 2012 if(!preg_match('/{3,20}/', $username)){ The construct {x,y} in a regex means that the previous item can occur a minimum of x times and a maximum of y times. In your case, there is no previous item so the construct does not make sense. That is why you are getting the warning. You need to define what pattern it is you want that repetition construct to apply to first. Judging by your error message, all your trying to do is check the length of the username in this line. Rather than use a regex you should just use strlen for that job. $len = strlen($username); if ($len < 3 || $len > 20){ //error } As for your foreach error, when you are assigning your error messages, your changing $this->error from an array to a string, and foreach cannot operate on a string. If you want to append the error messages to your error array, you need to assign them like so: $this->error[] = 'Your username must be 3 to 20 characters in length.'; Quote Link to comment https://forums.phpfreaks.com/topic/261069-php-foreach-classes-regex/#findComment-1337963 Share on other sites More sharing options...
Far Cry Posted April 17, 2012 Author Share Posted April 17, 2012 Thank you so much, guys. It really is the little things that matter. Quote Link to comment https://forums.phpfreaks.com/topic/261069-php-foreach-classes-regex/#findComment-1337964 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.