Skip to content

Instantly share code, notes, and snippets.

@tobipch
Forked from 140bytes/LICENSE.txt
Last active October 17, 2023 18:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobipch/8849169 to your computer and use it in GitHub Desktop.
Save tobipch/8849169 to your computer and use it in GitHub Desktop.
97 Byte International Phone Number Validator

Phone Number Validator in 97 Bytes

97 Byte Code

function(p){return/^(\+|\()\d{2,3}\)?\s\d{2,3}(\s|\-)\d{3,7}\s?(\d{1,3})?\s?(\d{1,3})?$/.test(p)}

Examples

function validPhoneNumber(p){return/^(\+|\()\d{2,3}\)?\s\d{2,3}(\s|\-)\d{3,7}\s?(\d{1,3})?\s?(\d{1,3})?$/.test(p)} //With Function Name +17 Bytes

validPhoneNumber("(123) 456-7890"); //TRUE
validPhoneNumber("+49 89 1234567"); //TRUE
validPhoneNumber("+49 89 12345 0"); //TRUE
validPhoneNumber("+49 89 12345 123"); //TRUE
validPhoneNumber("+49 89 123 456 789"); //TRUE
validPhoneNumber("49 89 12345 123"); //FALSE
validPhoneNumber("+49 89 123 456 789 00"); //FALSE
validPhoneNumber("(1233) 456-7890"); //FALSE
validPhoneNumber("(123) 4567890"); //FALSE
validPhoneNumber("123 456-7890"); //FALSE
//etc.

Demo

Demo at http://jsfiddle.net/y3msq/5/

function(p){
return //returns true or false
/^(\+|\()\d{2,3}\)?\s\d{2,3}(\s|\-)\d{3,7}\s?(\d{1,3})?\s?(\d{1,3})?$/ //Regular Expression (regex) to evaluate the phone number --> see http://regex101.com/r/fX9mU4
.test(p) //Lets the created regex evaluate the phone number who's give as function paremeter
}
function(p){return/^(\+|\()\d{2,3}\)?\s\d{2,3}(\s|\-)\d{3,7}\s?(\d{1,3})?\s?(\d{1,3})?$/.test(p)}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Tobias Peter (tobip.ch)
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "Phone Number Validator",
"description": "97 Byte Sized Phone Number Validator. Returns true or false depending on whether the phone number is valid or not. Works for international and US-Numbers.
"keywords": [
"phone",
"number",
"validator",
"phone number",
"validaton",
"international",
"140Byt.es"
]
}
<!doctype html>
<title>Phone Number Validator</title>
<input type="text" id="phoneNumberInput" placeholder="Phone Number..." />
<input type="button" id="validateBtn" value="Validate" onclick="validate()" />
<h2 id="validationOutput"></h2>
<script>
var input = document.getElementById("phoneNumberInput");
var output = document.getElementById("validationOutput");
function validate(){
/^(\+|\()\d{2,3}\)?\s\d{2,3}(\s|\-)\d{3,7}\s?(\d{1,3})?\s?(\d{1,3})?$/.test(input.value)?output.textContent="valid":output.textContent="invalid";
}
</script>
@atk
Copy link

atk commented Feb 7, 2014

What about international phone numbers?

@tsaniel
Copy link

tsaniel commented Feb 8, 2014

The trailing semicolon and space after return are not needed.

@tobipch
Copy link
Author

tobipch commented Feb 13, 2014

@atk: Thx for the idea. I realized it, referencing to the ITU E.123 Standard.
@tsaniel: Nice Tip! Thank you!

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