Page 1 of 1

php preg-match is returning only false

Posted: Wed Apr 15, 2015 9:37 am
by terror2012
Hello. I started to make a login system for my website. Everything was going a bit ok. I got some good tutorials on internet (I'm still beginner on php). Until now, I did this script, also register script but that script has some syntax problems but I'll make it works.
Now I have a problem with login script.
I have a code:

Code: Select all

return (preg_match('/^[a-zA-Z0-9](5,12)$/',$this->_username) && preg_match('/^[a-zA-Z0-9](5,12)$/',$this->_password)) ? 1 : 0;
This should return true if the user and pass are ok.
Now I have this code:

Code: Select all

if(!$this->isDataValid())
throw new Exception('Invalid Form Data');
Ok, so Even if I put a user and password like Test123, I got this error: Errors

Invalid Form Data

For live preview: http://astral-gaming.com/login.php

Can someone help me to make this login work? I just have no ideea why I get this error.

Re: php preg-match is returning only false

Posted: Wed Apr 15, 2015 9:57 am
by Celauran
You want braces, not parentheses. Also, restricting password length is a pretty bad idea.

Re: php preg-match is returning only false

Posted: Thu Apr 16, 2015 8:11 am
by terror2012
Hello. Still not working. Can you please insert the correct code? I think I didn't put the braces correctly.

Re: php preg-match is returning only false

Posted: Thu Apr 16, 2015 9:03 pm
by requinix
The only parentheses in the regexes are with the (5,12).

Code: Select all

return (preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_username) && preg_match('/^[a-zA-Z0-9]{5,12}$/',$this->_password)) ? 1 : 0;
+1 to not restricting the password length. And allowing only letters and numbers is obscene.