dadamssg Posted March 23, 2009 Share Posted March 23, 2009 i want the user to be able to use apostrophes(') and dashes(-) in this...where would i add those characters? thanks if(!preg_match('/^[a-z0-9]{8,12}$/i',$_POST['username'])) Quote Link to comment https://forums.phpfreaks.com/topic/150656-add-apostrophe-and-dash-in-preg_match/ Share on other sites More sharing options...
redarrow Posted March 23, 2009 Share Posted March 23, 2009 tell me if it works please. if(!preg_match('/^[\'\-a-z0-9]{8,12}$/i',$_POST['username'])) Quote Link to comment https://forums.phpfreaks.com/topic/150656-add-apostrophe-and-dash-in-preg_match/#findComment-791476 Share on other sites More sharing options...
dadamssg Posted March 23, 2009 Author Share Posted March 23, 2009 hey thanks, it worked for the dash but not for the apostrophe. i tried this but it didn't help if(!preg_match('/^[\'\-\a-z0-9]{8,12}$/i',$_POST['username'])) Quote Link to comment https://forums.phpfreaks.com/topic/150656-add-apostrophe-and-dash-in-preg_match/#findComment-791492 Share on other sites More sharing options...
redarrow Posted March 23, 2009 Share Posted March 23, 2009 what about this then tell me if(!preg_match('/^[\-\a-z0-9]\'{8,12}$/i',$_POST['username'])) Quote Link to comment https://forums.phpfreaks.com/topic/150656-add-apostrophe-and-dash-in-preg_match/#findComment-791496 Share on other sites More sharing options...
dadamssg Posted March 23, 2009 Author Share Posted March 23, 2009 nope, any other ideas? if(!preg_match('/^[\'\a-z0-9]\'{8,12}$/i',$_POST['username'])) Quote Link to comment https://forums.phpfreaks.com/topic/150656-add-apostrophe-and-dash-in-preg_match/#findComment-791497 Share on other sites More sharing options...
.josh Posted March 24, 2009 Share Posted March 24, 2009 if(!preg_match("/^[-'a-z0-9]{8,12}$/i",$_POST['username'])) You may need to stripslashes $_POST['username'] before preg_matching though. Also, that pattern does not prevent stuff like this from matching: a-----bc a''''''''bc a-b-c-d-e ------abc-- etc.. Quote Link to comment https://forums.phpfreaks.com/topic/150656-add-apostrophe-and-dash-in-preg_match/#findComment-792236 Share on other sites More sharing options...
dadamssg Posted March 24, 2009 Author Share Posted March 24, 2009 hey thanks, do you know how to restrict those characters to only being able to appear once? Im trying to account for names like O'hara or something of the sort Quote Link to comment https://forums.phpfreaks.com/topic/150656-add-apostrophe-and-dash-in-preg_match/#findComment-792280 Share on other sites More sharing options...
Salkcin Posted March 29, 2009 Share Posted March 29, 2009 Well, you could use substr_count() to count how many times a char appears in a text. ex: $user = $_POST['username']; if (!preg_match("/^[-'a-z0-9]{8,12}$/i",$user) || substr_count($user, "'") > 1 || substr_count($user, "-") > 1) { echo "Invalid"; } else { echo "Valid"; } Quote Link to comment https://forums.phpfreaks.com/topic/150656-add-apostrophe-and-dash-in-preg_match/#findComment-796032 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.