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
Post a Comment