acctman Posted April 8, 2008 Share Posted April 8, 2008 hi i'm using this code to check for illegal characters in a username, i'd like to add a hypen and underscore as allowed characters. //illegal characters in username? if (!ctype_alnum($en['user'])) $err .= _reg_illegal.'<br>'; Link to comment https://forums.phpfreaks.com/topic/100238-illegal-characters/ Share on other sites More sharing options...
EXiT Posted April 9, 2008 Share Posted April 9, 2008 This works for me... if(!preg_match('/^[a-zA-Z0-9_-]+$/', $en['user'])) { ...Error Msg } Link to comment https://forums.phpfreaks.com/topic/100238-illegal-characters/#findComment-512525 Share on other sites More sharing options...
acctman Posted April 9, 2008 Author Share Posted April 9, 2008 This works for me... if(!preg_match('/^[a-zA-Z0-9_-]+$/', $en['user'])) { ...Error Msg } so the above code would do a-Z 0-9 plus - and _ everything else would be rejected right? what's the +$ for? Link to comment https://forums.phpfreaks.com/topic/100238-illegal-characters/#findComment-512533 Share on other sites More sharing options...
discomatt Posted April 9, 2008 Share Posted April 9, 2008 That's wrong. You must escape the hyphen or have it FIRST /^[a-zA-Z0-9_\-]+$/ or /^[-a-zA-Z0-9_]+$/ + is a quantifier. It says match the class between 1 and infinite times, as many times as possible, giving back as needed. $ matches the end of the string. Link to comment https://forums.phpfreaks.com/topic/100238-illegal-characters/#findComment-512540 Share on other sites More sharing options...
acctman Posted April 9, 2008 Author Share Posted April 9, 2008 That's wrong. You must escape the hyphen or have it FIRST /^[a-zA-Z0-9_\-]+$/ or /^[-a-zA-Z0-9_]+$/ + is a quantifier. It says match the class between 1 and infinite times, as many times as possible, giving back as needed. $ matches the end of the string. so the correct code would be if(!preg_match('/^[a-zA-Z0-9_\-]+$/', $en['user'])) $err .= _reg_illegal.'<br>'; Link to comment https://forums.phpfreaks.com/topic/100238-illegal-characters/#findComment-512565 Share on other sites More sharing options...
discomatt Posted April 9, 2008 Share Posted April 9, 2008 Give it a shot. Looks good from here Link to comment https://forums.phpfreaks.com/topic/100238-illegal-characters/#findComment-512567 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.