Skip to content

Instantly share code, notes, and snippets.

@justsml
Created January 20, 2015 05:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justsml/8623758d0b34cdb6994c to your computer and use it in GitHub Desktop.
Save justsml/8623758d0b34cdb6994c to your computer and use it in GitHub Desktop.
Handy RegEx Validation Snippit
/*
Credit and Source: https://www.owasp.org/index.php/OWASP_Validation_Regex_Repository
*/
'use strict';
module.exports = {
url: /^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:@&=+$,A-Za-z0-9])+)([).!';\/?:,][[:blank:]])?$/,
ip: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
email: /^[a-zA-Z0-9+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,7}$/,
safeText: /^[a-zA-Z0-9 .-]+$/,
date: /^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/,
creditCard: /^((4\d{3})|(5[1-5]\d{2})|(6011)|(7\d{3}))-?\d{4}-?\d{4}-?\d{4}|3[4,7]\d{13}$/,
password: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$/,
complexPassword: /^(?:(?=.*\d)(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[^A-Za-z0-9])(?=.*[a-z])|(?=.*[^A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z])|(?=.*\d)(?=.*[A-Z])(?=.*[^A-Za-z0-9]))(?!.*(.)\1{2,})[A-Za-z0-9!~<>,;:_=?*+#."&§%°()\|\[\]\-\$\^\@\/]{8,32}$/,
zipCode: /^\d{5}(-\d{4})?$/,
phone: /^\D?(\d{3})\D?\D?(\d{3})\D?(\d{4})$/,
ssn: /^\d{3}-\d{2}-\d{4}$/,
filename: {
windows: /^(?!^(PRN|AUX|CLOCK\$|NUL|CON|COM\d|LPT\d|\..*)(\..+)?$)[^\x00-\x1f\\?*:\";|\/]+$/,
linux: /^(([a-zA-Z]:|\\)\\)?(((\.)|(\.\.)|([^\\\/:*?"|<>. ](([^\\\/:*?"|<>. ])|([^\\\/:*?"|<>]*[^\\\/:*?"|<>. ]))?))\\)*[^\\\/:*?"|<>. ](([^\\\/:*?"|<>. ])|([^\\\/:*?"|<>]*[^\\\/:*?"|<>. ]))?$/
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment