PrinceOfDragons Posted August 11, 2009 Share Posted August 11, 2009 Is this an efficient way to check my form field? function checkUname() { global $uname; if($this->username == "") { echo "Username field is blank"; $this->myErrors++; } elseif($uname != $_POST['username'] && $_POST['Log-In'] == "Log-In"){ echo "Username does not exist"; $this->myErrors++; } elseif(strlen($this->username) < "5") { echo "Username is to short"; $this->myErrors++; } elseif(!eregi("^([0-9a-z])*$", $this->username)) { echo "Username is not Alphanumeric"; $this->myErrors++; } elseif($uname == true && $_SESSION['create'] == "Create Account"){ echo "Username Taken"; $this->myErrors++; Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 11, 2009 Share Posted August 11, 2009 elseif(strlen($this->username) < 5 || strlen($this->username) > 15) Should be 5, "5" is a string. You should add a max amount of characters.. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted August 11, 2009 Share Posted August 11, 2009 if($this->username == "") { could be changed to if(empty($this->username)) { Quote Link to comment Share on other sites More sharing options...
oni-kun Posted August 11, 2009 Share Posted August 11, 2009 function checkUname() { global $uname; if(empty($this->username)) { echo "Username field is blank"; $this->myErrors++; } elseif($uname != $_POST['username'] && $_POST['Log-In'] == "Log-In"){ echo "Username does not exist"; $this->myErrors++; } elseif(strlen($this->username) < 5 || strlen($this->username) > 10) { echo "Username is to short"; $this->myErrors++; } elseif(!preg_match('/[^0-9a-zA-Z]/s', $this->username)) { echo "Username is not Alphanumeric"; $this->myErrors++; } elseif($uname == true && $_SESSION['create'] == "Create Account"){ echo "Username Taken"; $this->myErrors++; That should be better. Eregi is deprecated so it should be preg_match. Quote Link to comment Share on other sites More sharing options...
PrinceOfDragons Posted August 11, 2009 Author Share Posted August 11, 2009 Thanks for the help as I am new to php, I do not know the most efficent way to use the code but I can get it to do what I want . 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.