Last active
October 13, 2015 03:08
-
-
Save cbumgard/4129847 to your computer and use it in GitHub Desktop.
"gist" URL jQuery validator method for form inputs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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