Skip to content

Instantly share code, notes, and snippets.

@allenfantasy
Last active May 2, 2016 13:58
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 allenfantasy/dd5e5c97554a5c5983f2 to your computer and use it in GitHub Desktop.
Save allenfantasy/dd5e5c97554a5c5983f2 to your computer and use it in GitHub Desktop.
regex.js
/***** Fixed Phone Number Regex *****/
/*
Fixed phone numbers' format:
(district number)-(phone number)
*/
var phoneRegex = /^(0[\d]{2,3}-)?[\d]{7,8}$/;
phoneRegex.test("020-82376643"); //=> true
phoneRegex.test("0755-6633868"); //=> true
phoneRegex.test("82394401"); //=> true
phoneRegex.test("122-82376643"); //=> false: district number must start with '0'
phoneRegex.test("823766443"); //=> false: phone number's length should be between 7 to 8.
/***** Mobile Phone Number Regex *****/
/*
Mobile phone number has length of 11.
And the first 3 digits should be 13x, 15x, 18x, 147 or 170.
*/
var mobileRegex = /^(1[358]\d|170|147){1}\d{8}$/;
mobileRegex.test("13427525363"); //=> true
mobileRegex.test("15023656987"); //=> true
mobileRegex.test("134878923890"); //=> false
/***** Email Address Regex *****/
/*
1. The completed and strongest(also the longest) one was introduced here: http://ex-parrot.com/~pdw/Mail-RFC822-Address.html
2. While there's a must simpler RegExp which can cover most of common cases: http://www.regular-expressions.info/email.html
*/
/*
TODO
1. RFC 2822
2. RFC 822
3. RFC 5322
*/
/* REFERENCES
1. Mail::RFC822::Address: regexp-based address validation: http://ex-parrot.com/~pdw/Mail-RFC822-Address.html
2. From Stackoverflow: http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
3. Another thread from Stackoverflow(better): http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address
4. RFC-compliant email address validation: http://isemail.info/_system/is_email/test/?all
5. RegularExpression.info, How to Find or Validate an Email Address: http://www.regular-expressions.info/email.html
*/
var emailRegex = /^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i;
emailRegex.test("Allen@dxhackers.org"); //=> true
emailRegex.test("allen.xx@dx.org"); //=> true
emailRegex.test("a.b.c@d.com"); //=> true
emailRegex.test("allen@dx"); //=> false
@Piskvor
Copy link

Piskvor commented May 2, 2016

{2,4} ? People at info@dot.swiss are unhappy, but hey, that's a new domain at least. info@about.museum has been active for 15 years now, enough with the 4-character TLD folklore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment