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

View File

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

View File

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