Skip to content

Instantly share code, notes, and snippets.

@evadne
Created February 21, 2010 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evadne/310065 to your computer and use it in GitHub Desktop.
Save evadne/310065 to your computer and use it in GitHub Desktop.
monoValidate (yup, with an easter egg)
// monoValidate.js by Evadne Wu, this version retrieved 2010. 02. 21. New BSD.
// Library required: jQuery. Evadne is reachable thru monoceroi.com.
// Notice: malformed URIs galore and our URI schema has not been tested against with many real-world cases yet.
function monoValidate(stringToBeValidated, proposedCategory) {
// So we can leniently call monoValidate to validate against nothing; that way everything that is not required to be validated is valid
if (stringToBeValidated == '' || proposedCategory == '') return true;
var options = $.extend({
'caseSensitive': false
}, arguments[2]);
var schemata = {
'email': new RegExp(/[a-z0-9!#$%&'*+//=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+//=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[a-z]{2}||se|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum|local)\b/),
'uri': new RegExp(/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/)
}
if (!options.caseSensitive) stringToBeValidated = String(stringToBeValidated).toLowerCase();
if (schemata[String(proposedCategory)] == undefined) return false;
return (String(stringToBeValidated).match(schemata[String(proposedCategory)]) != null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment