Jump to content

PHP foreach, classes, Regex


Far Cry

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.