Skip to content

jQuery Validator: Twitter username validator

Aug 7, 2012 1 min read #javascript #jquery

This post is more than 18 months old. Technology changes quickly, so some of this content may be out of date (but that's not always the case). Please verify any technical or programming information against the current release.

If you use the jQuery Validation plugin, and of course, you follow an amazing PHP Programmer and now have twitter boxes on all your forms, you might need to validate it some day. I wrote this method for it.

jQuery.validator.addMethod("twitterhandle", 
  function(value) {
    if(value == "") return true;
    var pattern = /^\w{1,32}$/;
    var result = pattern.test(value);
    return result;
  }, 
  "Twitter handle only - no spaces, @, and not a URL."
);
javascript