Modified validatePhone to accept a string or an object. If it is a string, it handles it as a UK phone number.

This commit is contained in:
Martin Donnelly 2016-03-16 11:04:34 +00:00
parent 5dd441a06e
commit 7100cf5ed2
3 changed files with 30 additions and 19 deletions

View File

@ -7,7 +7,8 @@
* *
*/ */
/* exported VALIDATE */ /* exported VALIDATE */
var VALIDATE = new function() { var VALIDATE;
VALIDATE = new function() {
this.dateBuilder = function(d, m, y) { this.dateBuilder = function(d, m, y) {
return new Date(Date.UTC(y, m, d, 0, 0, 0)); return new Date(Date.UTC(y, m, d, 0, 0, 0));
@ -213,15 +214,15 @@ var VALIDATE = new function() {
// Remove spaces from the telephone number to help validation // Remove spaces from the telephone number to help validation
while (telnum.indexOf(' ') !== -1) { while (telnum.indexOf(' ') !== -1) {
telnum = telnum.slice(0, telnum = telnum.slice(0,
telnum.indexOf(' ')) + telnum.slice(telnum.indexOf(' ') + 1); telnum.indexOf(' ')) + telnum.slice(telnum.indexOf(' ') + 1);
} }
// Remove hyphens from the telephone number to help validation // Remove hyphens from the telephone number to help validation
while (telnum.indexOf('-') !== -1) { while (telnum.indexOf('-') !== -1) {
telnum = telnum.slice(0, telnum = telnum.slice(0,
telnum.indexOf('-')) + telnum.slice(telnum.indexOf('-') + 1); telnum.indexOf('-')) + telnum.slice(telnum.indexOf('-') + 1);
} }
// Now check that all the characters are digits // Now check that all the characters are digits
@ -273,15 +274,25 @@ var VALIDATE = new function() {
}; };
/** /**
* *
* @param params * @param params
* @returns {string} * @returns {string}
*/ */
this.validatePhone = function(params) { this.validatePhone = function(params) {
var uk = params.uk || false; var uk, v, required, itrx;
var v = params.number; if (typeof params === 'object') {
var required = params.req || false; uk = params.uk || false;
var itrx; v = params.number;
required = params.req || false;
}
if (typeof params === 'string') {
uk = true;
v = params;
required = false;
}
var onlyDigits = function(inval) { var onlyDigits = function(inval) {

View File

@ -1,6 +1,6 @@
{ {
"name": "node-validator", "name": "node-validator",
"version": "0.1.0", "version": "0.1.1",
"description": "A collection of validators.", "description": "A collection of validators.",
"main": "md-validator.js", "main": "md-validator.js",
"scripts": { "scripts": {

View File

@ -21,9 +21,9 @@ describe('Phone Validator', function() {
done(); done();
}); });
it('should validate a bracketted uk number', function(done) { it('should validate a number with no options', function(done) {
assert.equal($V.validatePhone({number: '(0)1389 602003', uk: true}), assert.equal($V.validatePhone('01389 602001'),
'01389602003'); '01389602001');
done(); done();
}); });