Updated phone validator to return empty for null or undefined objects

This commit is contained in:
Martin Donnelly 2016-03-18 11:44:15 +00:00
parent 1e7e87a62d
commit fdfb9bc71b
3 changed files with 61 additions and 0 deletions

46
.jscsrc Normal file
View File

@ -0,0 +1,46 @@
{
"disallowKeywords": ["with"],
"disallowKeywordsOnNewLine": ["else"],
"disallowMixedSpacesAndTabs": true,
"disallowMultipleVarDecl": "exceptUndefined",
"disallowNewlineBeforeBlockStatements": true,
"disallowQuotedKeysInObjects": true,
"disallowSpaceAfterObjectKeys": true,
"disallowSpaceAfterPrefixUnaryOperators": true,
"disallowSpacesInFunction": {
"beforeOpeningRoundBrace": true
},
"disallowSpacesInsideParentheses": true,
"disallowTrailingWhitespace": true,
"maximumLineLength": 160,
"requireCamelCaseOrUpperCaseIdentifiers": false,
"requireCapitalizedComments": true,
"requireCapitalizedConstructors": true,
"requireCurlyBraces": true,
"requireSpaceAfterKeywords": [
"if",
"else",
"for",
"while",
"do",
"switch",
"case",
"return",
"try",
"catch",
"typeof"
],
"requireSpaceAfterLineComment": true,
"requireSpaceAfterBinaryOperators": true,
"requireSpaceBeforeBinaryOperators": true,
"requireSpaceBeforeBlockStatements": true,
"requireSpaceBeforeObjectValues": true,
"requireSpacesInFunction": {
"beforeOpeningCurlyBrace": true
},
"requireTrailingComma": false,
"requireEarlyReturn": false,
"validateIndentation": 2,
"validateLineBreaks": "LF",
"validateQuoteMarks": "'"
}

View File

@ -265,6 +265,11 @@ VALIDATE = new function() {
this.validatePhone = function(params) {
var onlyDigits;
var uk, v, required, itrx;
if ((typeof params === 'undefined') || (typeof params === 'object' && params === null)) {
return '';
}
if (typeof params === 'object') {
uk = params.uk || false;
v = params.number;

View File

@ -9,6 +9,16 @@ describe('Phone Validator', function() {
done();
});
it('should not validate an non existing object', function(done) {
assert.equal($V.validatePhone(), '');
done();
});
it('should not validate an null object', function(done) {
assert.equal($V.validatePhone(null), '');
done();
});
it('should validate an normal uk number', function(done) {
assert.equal($V.validatePhone({number: '01389 602001', uk: true}),
'01389602001');