tkm76 Posted December 31, 2010 Share Posted December 31, 2010 Hi everybody! I'm having a bit of a problem with preg_match. I've spent two days on this, tried dozens of ways, nothing worked. So I decided to join this forum. OK, my problem is in these lines of code: if (!preg_match("/^[a-zA-Z0-9_\-\.]*$/", $username)){ echo "Username contains excluded characters!"; exit; } I'm making a registration form, and I want to allow following types of registration: "john_doe", "john-doe" and "john.doe". Besides "johndoe". Case sensitivity and numbers are also included. Everything other than that should be considered as excluded. I somehow managed to register two dummy records as it is. But after that it started giving me that echo message" Username contains excluded characters!" if i DON'T user those three special characters as well! And that shouldn't be a condition, but an option. How do I solve this problem? I would be grateful if someone would correct me in whatever I'm doing wrong. Thanks in advance! T.K.M. Link to comment https://forums.phpfreaks.com/topic/223073-preg_match-excluded-characters/ Share on other sites More sharing options...
johnny86 Posted December 31, 2010 Share Posted December 31, 2010 Try: if(!preg_match('#^[\w\.-]+$#i', $username)) { echo 'illegal characters!'; } Link to comment https://forums.phpfreaks.com/topic/223073-preg_match-excluded-characters/#findComment-1153315 Share on other sites More sharing options...
tkm76 Posted December 31, 2010 Author Share Posted December 31, 2010 I just tested it. It works flawlessly. Thank you! Link to comment https://forums.phpfreaks.com/topic/223073-preg_match-excluded-characters/#findComment-1153323 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.