php - RegEx for Twitter username not working -


the following regular expression function validating twitter username not working because twitter name can minimum of 1 character , maximum of 20. however, when tested this, allows usernames greater 20 characters. did go wrong?

public function val_username($subject) {     return (bool)preg_match('/[a-za-z0-9_]{1,20}/', $subject);       } 

you forgot $ , ^

/^[a-za-z0-9_]{1,20}$/ should work

public function val_username($subject) {     return (bool)preg_match('/^[a-za-z0-9_]{1,20}$/', $subject);       } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -