Skip to content

Instantly share code, notes, and snippets.

@cbumgard
Last active October 13, 2015 03:08
Show Gist options
  • Save cbumgard/4129847 to your computer and use it in GitHub Desktop.
Save cbumgard/4129847 to your computer and use it in GitHub Desktop.
"gist" URL jQuery validator method for form inputs
<!-- Put a text input to be validated into a form. Make sure it has the class "gist" for the validator. -->
<input id="inputGistUrl" type="text" name="gist_url" placeholder="https://gist.github.com/1234567" class="required gist">
// Prepare the form for validation:
$('form#id').validate();
// Add a custom form validator method for Gist URLs (e.g. "https://gist.github.com/cbumgard/4129847")
// The username must match the following rules according to GitHub: Username may only contain alphanumeric characters or dashes and cannot begin with a dash
// The Gist ID contains digits and possibly hex characters a-f.
// For more on customer validator methods, see http://docs.jquery.com/Plugins/Validation/Validator/addMethod#namemethodmessage
$.validator.addMethod('gist', function(value, element) {
return this.optional(element) || /^https:\/\/gist.github.com\/[A-Za-z0-9][A-Za-z0-9-]*\/[a-f\d]+$/.test(value);
}, 'Please enter a valid Gist URL.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment