SO 4338267: Validate phone number with regex
http://stackoverflow.com/questions/4338267/validate-phone-number-with-javascript
HTML
<script src="http://code.jquery.com/qunit/qunit-1.13.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.13.0.css">
<div id="qunit"></div>
<div id="qunit-fixture"></div>
JavaScript
//return phone.match(/([0-9]{11}|[0-9]{5} [0-9]{3} [0-9]{3})/);
var validate = {
telephone: function telephone(input) {
return /^\(?(\d{3})\)?[ .-]?(\d{3})[ .-]?(\d{4})$/.test(input)
}
}
test("08000000000", function () {
var input = "08000000000";
ok(validate.telephone(input) === true);
});
test("08000 000 000", function () {
var input = "08000 000 000";
ok(validate.telephone(input) === true);
});
test("null", function () {
var input = null;
ok(validate.telephone(input) === false);
});
test("empty string", function () {
var input = '';
ok(validate.telephone(input) === false);
});
test("alphanumeric", function () {
var input = 'abc123';
ok(validate.telephone(input) === false);
});