kwame123 Posted June 18, 2017 Share Posted June 18, 2017 this is my code $re = '/[^\W][a-zA-Z\d.-]{3,20}/'; if(!preg_match_all($re, $username)){ do something }else{ do somthing else } $username is a $_POST['username']; when i submit a 3 letter word it works but when i use symbols like $ and ! and etc it still goes through, im trying to get a regex where the username is 4-20 characters long and only the alphabet and numbers and dots and dash but dots and dashs cant be next to each other thx Quote Link to comment https://forums.phpfreaks.com/topic/304164-regex-not-working/ Share on other sites More sharing options...
Solution Jacques1 Posted June 18, 2017 Solution Share Posted June 18, 2017 (edited) Your regex doesn't say that at all. You have no anchors (which means a substring match is sufficient), and for some strange reason you're using preg_match_all(), as if you expected multiple matches. If you only want to allow dashes and dots as delimiters between alphanumerics, that's '/\\A[a-z\\d]+(?:[.-][a-z\\d]+)*\\z/i' Length checks are the job of strlen() or mb_strlen(). Edited June 18, 2017 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/304164-regex-not-working/#findComment-1547504 Share on other sites More sharing options...
kwame123 Posted June 18, 2017 Author Share Posted June 18, 2017 Your regex doesn't say that at all. You have no anchors (which means a substring match is sufficient), and for some strange reason you're using preg_match_all(), as if you expected multiple matches. If you only want to allow dashes and dots as delimiters between alphanumerics, that's '/\\A[a-z\\d]+(?:[.-][a-z\\d]+)*\\z/i' Length checks are the job of strlen() or mb_strlen(). oh my goodness thank you so much. not its fixed! Quote Link to comment https://forums.phpfreaks.com/topic/304164-regex-not-working/#findComment-1547506 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.