Skip to content

Instantly share code, notes, and snippets.

@beckettkev
Created February 13, 2017 09:40
Show Gist options
  • Save beckettkev/498f43d461a0308f66898d936b595a75 to your computer and use it in GitHub Desktop.
Save beckettkev/498f43d461a0308f66898d936b595a75 to your computer and use it in GitHub Desktop.
Validation Regular Expressions for JavaScript
//Between 1 - 255 characters...
/^.{1,255}$/
//Letters only with spaces (no numbers or special characters)...
/^[a-zA-Z\s]*$/
//Number required...
/^\d+$/
//Number to two decimal places...
/\d\.\d{2,2}?$/
//Number with dashes allowed...
/^(\d+-?)+\d+$/
//Currency with optional £ prefix...
/^\£?(([1-9][0-9]{0,2}(,[0-9]{3})*)|[0-9]+)?\.[0-9]{1,2}$/
//Percentage with no decimal places (with opitional percentage sign)...
/^([0-9]|[1-9][0-9]|100)(%?)?$/
//Percentage with one decimal place (with opitional percentage sign)...
/^(\d{0,2}(\.\d)|100(\.0))(%?)?$/
//Percentage with two decimal places (with opitional percentage sign)...
/^(\d{0,2}(\.\d{2,2})|100(\.00))(%?)?$/
//Date in english format
/^(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)?[0-9]{2}$/
//Hyperlink
/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/
//Email
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
//File extension validation...
/\.pdf|\.doc|\.docx|\.rtf$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment