php preg-match is returning only false

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
terror2012
Forum Newbie
Posts: 3
Joined: Wed Apr 15, 2015 9:32 am

php preg-match is returning only false

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php preg-match is returning only false

Post by Celauran »

You want braces, not parentheses. Also, restricting password length is a pretty bad idea.
terror2012
Forum Newbie
Posts: 3
Joined: Wed Apr 15, 2015 9:32 am

Re: php preg-match is returning only false

Post by terror2012 »

Hello. Still not working. Can you please insert the correct code? I think I didn't put the braces correctly.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: php preg-match is returning only false

Post 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.
Post Reply