mirror of
https://gitlab.silvrtree.co.uk/martind2000/node-validator.git
synced 2025-01-25 16:06:16 +00:00
Added jshint test and tidied code to pass the test
This commit is contained in:
parent
2b46220f00
commit
cdd1af6eaf
32
.jshintrc
Normal file
32
.jshintrc
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
"predef": [
|
||||||
|
"Promise"
|
||||||
|
],
|
||||||
|
"node":true,
|
||||||
|
"browser": false,
|
||||||
|
"boss": true,
|
||||||
|
"curly": true,
|
||||||
|
"debug": false,
|
||||||
|
"devel": true,
|
||||||
|
"eqeqeq": true,
|
||||||
|
"evil": true,
|
||||||
|
"forin": false,
|
||||||
|
"immed": false,
|
||||||
|
"laxbreak": false,
|
||||||
|
"newcap": true,
|
||||||
|
"noarg": true,
|
||||||
|
"noempty": false,
|
||||||
|
"nonew": false,
|
||||||
|
"nomen": false,
|
||||||
|
"onevar": false,
|
||||||
|
"plusplus": false,
|
||||||
|
"regexp": false,
|
||||||
|
"undef": true,
|
||||||
|
"sub": true,
|
||||||
|
"strict": false,
|
||||||
|
"white": false,
|
||||||
|
"eqnull": true,
|
||||||
|
"esnext": true,
|
||||||
|
"unused": true,
|
||||||
|
"supernew":true
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
"use strict";
|
'use strict';
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* User: Martin Donnelly
|
* User: Martin Donnelly
|
||||||
@ -6,6 +6,7 @@
|
|||||||
* Time: 14:26
|
* Time: 14:26
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
/* exported VALIDATE */
|
||||||
var VALIDATE = new function() {
|
var VALIDATE = new function() {
|
||||||
|
|
||||||
this.dateBuilder = function(d, m, y) {
|
this.dateBuilder = function(d, m, y) {
|
||||||
@ -49,9 +50,15 @@ var VALIDATE = new function() {
|
|||||||
flag = !!/^\w+([\+\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email);
|
flag = !!/^\w+([\+\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email);
|
||||||
|
|
||||||
b = email.split("@");
|
b = email.split("@");
|
||||||
if (b.length > 2) flag = false;
|
if (b.length > 2) {
|
||||||
if (b[0].length == 0) flag = false;
|
flag = false;
|
||||||
if (email.charAt(0) == '.' || email.charAt(email.length - 1) == '.') flag = false;
|
}
|
||||||
|
if (b[0].length === 0) {
|
||||||
|
flag = false;
|
||||||
|
}
|
||||||
|
if (email.charAt(0) === '.' || email.charAt(email.length - 1) === '.') {
|
||||||
|
flag = false;
|
||||||
|
}
|
||||||
return flag;
|
return flag;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -78,7 +85,6 @@ var VALIDATE = new function() {
|
|||||||
* @returns {boolean}
|
* @returns {boolean}
|
||||||
*/
|
*/
|
||||||
this.money = function(inval) {
|
this.money = function(inval) {
|
||||||
"use strict";
|
|
||||||
var rtrn = '', exp = /^\xA3?\d{1,3}?([,]\d{3}|\d)*?([.]\d{1,2})?$/;
|
var rtrn = '', exp = /^\xA3?\d{1,3}?([,]\d{3}|\d)*?([.]\d{1,2})?$/;
|
||||||
|
|
||||||
if (exp.test(inval)) {
|
if (exp.test(inval)) {
|
||||||
@ -160,7 +166,7 @@ var VALIDATE = new function() {
|
|||||||
|
|
||||||
// If it is the Anguilla overseas territory postcode, we need to
|
// If it is the Anguilla overseas territory postcode, we need to
|
||||||
// treat it specially
|
// treat it specially
|
||||||
if (toCheck.toUpperCase() == 'AI-2640') {postCode = 'AI-2640'}
|
if (toCheck.toUpperCase() === 'AI-2640') {postCode = 'AI-2640';}
|
||||||
// Load new postcode back into the form element
|
// Load new postcode back into the form element
|
||||||
valid = true;
|
valid = true;
|
||||||
|
|
||||||
@ -178,7 +184,9 @@ var VALIDATE = new function() {
|
|||||||
// invalid postcode
|
// invalid postcode
|
||||||
//if (valid) {return postCode;} else return false;
|
//if (valid) {return postCode;} else return false;
|
||||||
if (valid) {return postCode;}
|
if (valid) {return postCode;}
|
||||||
else return '';
|
else {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -190,7 +198,7 @@ var VALIDATE = new function() {
|
|||||||
|
|
||||||
// Convert into a string and check that we were provided with something
|
// Convert into a string and check that we were provided with something
|
||||||
var telNumberErrorNo, telnum = telephoneNumber + ' ';
|
var telNumberErrorNo, telnum = telephoneNumber + ' ';
|
||||||
if (telnum.length == 1) {
|
if (telnum.length === 1) {
|
||||||
// telNumberErrorNo = 1;
|
// telNumberErrorNo = 1;
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@ -198,19 +206,19 @@ var VALIDATE = new function() {
|
|||||||
|
|
||||||
// Don't allow country codes to be included (assumes a leading "+")
|
// Don't allow country codes to be included (assumes a leading "+")
|
||||||
var exp = /^(\+)[\s]*(.*)$/;
|
var exp = /^(\+)[\s]*(.*)$/;
|
||||||
if (exp.test(telnum) == true) {
|
if (exp.test(telnum) === true) {
|
||||||
//telNumberErrorNo = 2;
|
//telNumberErrorNo = 2;
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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);
|
||||||
}
|
}
|
||||||
@ -218,14 +226,14 @@ var VALIDATE = new function() {
|
|||||||
// Now check that all the characters are digits
|
// Now check that all the characters are digits
|
||||||
|
|
||||||
exp = /^[0-9]{10,11}$/;
|
exp = /^[0-9]{10,11}$/;
|
||||||
if (exp.test(telnum) != true) {
|
if (exp.test(telnum) !== true) {
|
||||||
// telNumberErrorNo = 3;
|
// telNumberErrorNo = 3;
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now check that the first digit is 0
|
// Now check that the first digit is 0
|
||||||
exp = /^0[0-9]{9,10}$/;
|
exp = /^0[0-9]{9,10}$/;
|
||||||
if (exp.test(telnum) != true) {
|
if (exp.test(telnum) !== true) {
|
||||||
// telNumberErrorNo = 4;
|
// telNumberErrorNo = 4;
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@ -255,7 +263,7 @@ var VALIDATE = new function() {
|
|||||||
|
|
||||||
// Finally check that the telephone number is appropriate.
|
// Finally check that the telephone number is appropriate.
|
||||||
exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
|
exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
|
||||||
if (exp.test(telnum) != true) {
|
if (exp.test(telnum) !== true) {
|
||||||
// telNumberErrorNo = 5;
|
// telNumberErrorNo = 5;
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@ -265,8 +273,6 @@ var VALIDATE = new function() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.validatePhone = function(params) {
|
this.validatePhone = function(params) {
|
||||||
"use strict";
|
|
||||||
|
|
||||||
var uk = params.uk || false;
|
var uk = params.uk || false;
|
||||||
var v = params.number;
|
var v = params.number;
|
||||||
var required = params.req || false;
|
var required = params.req || false;
|
||||||
@ -278,49 +284,62 @@ var VALIDATE = new function() {
|
|||||||
|
|
||||||
for (var t = 0; t <= inval.length; t++) {
|
for (var t = 0; t <= inval.length; t++) {
|
||||||
ch = inval.charAt(t);
|
ch = inval.charAt(t);
|
||||||
if (allowed.indexOf(ch) != -1) ws = ws + ch;
|
if (allowed.indexOf(ch) !== -1) {
|
||||||
|
ws = ws + ch;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ws;
|
return ws;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (v.length == 0 && required == true) return '';
|
if (v.length === 0 && required === true) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
if (v.length > 0) {
|
if (v.length > 0) {
|
||||||
var internationalTest1 = /((\+|00)([0-9]{2,3}))(\(0\)|0)([1-9][0-9]{9})$/i;
|
var internationalTest1 = /((\+|00)([0-9]{2,3}))(\(0\)|0)([1-9][0-9]{9})$/i;
|
||||||
var internationalTest2 = /((\+|00)([0-9]{2,3}))([1-9][0-9]{9})$/i;
|
var internationalTest2 = /((\+|00)([0-9]{2,3}))([1-9][0-9]{9})$/i;
|
||||||
|
|
||||||
// tidy up international format numbers.
|
// tidy up international format numbers.
|
||||||
if (internationalTest1.test(v) == true) {
|
if (internationalTest1.test(v) === true) {
|
||||||
itrx = internationalTest1.exec(v);
|
itrx = internationalTest1.exec(v);
|
||||||
v = "00" + itrx[3] + itrx[5];
|
v = "00" + itrx[3] + itrx[5];
|
||||||
}
|
}
|
||||||
else if (internationalTest2.test(v) == true) {
|
else if (internationalTest2.test(v) === true) {
|
||||||
itrx = internationalTest2.exec(v);
|
itrx = internationalTest2.exec(v);
|
||||||
v = "00" + itrx[3] + itrx[4];
|
v = "00" + itrx[3] + itrx[4];
|
||||||
}
|
}
|
||||||
|
|
||||||
// strip invalid characters
|
// strip invalid characters
|
||||||
v = onlyDigits(v);
|
v = onlyDigits(v);
|
||||||
if (v.length < 8) return '';
|
if (v.length < 8) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
// only do this if it hasn't already failed
|
// only do this if it hasn't already failed
|
||||||
|
|
||||||
if (uk) {
|
if (uk) {
|
||||||
// perform uk specific tests
|
// perform uk specific tests
|
||||||
// de-internationalise the number
|
// de-internationalise the number
|
||||||
if (v.substr(0, 3) == "+44") v = "0" + v.substr(3, v.length);
|
if (v.substr(0, 3) === "+44") {
|
||||||
if (v.substr(0, 4) == "0044") v = "0" + v.substr(4, v.length);
|
v = "0" + v.substr(3, v.length);
|
||||||
|
}
|
||||||
|
if (v.substr(0, 4) === "0044") {
|
||||||
|
v = "0" + v.substr(4, v.length);
|
||||||
|
}
|
||||||
var pcv = this.checkUKTelephone(v);
|
var pcv = this.checkUKTelephone(v);
|
||||||
if (pcv === false) return '';
|
if (pcv === false) {
|
||||||
if (pcv != false) v = pcv;
|
return '';
|
||||||
|
}
|
||||||
|
if (pcv !== false) {
|
||||||
|
v = pcv;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// assume foreign
|
// assume foreign
|
||||||
|
|
||||||
if (!(v.charAt(0) == "+" || v.substr(0, 2) == "00")) {
|
if (!(v.charAt(0) === "+" || v.substr(0, 2) === "00")) {
|
||||||
// not right?
|
// not right?
|
||||||
|
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"private": true,
|
"private": true,
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"mocha": "^2.4.5"
|
"mocha": "^2.4.5",
|
||||||
|
"mocha-jshint": "^2.3.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
8
test/jshint.spec.js
Normal file
8
test/jshint.spec.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
*
|
||||||
|
* User: Martin Donnelly
|
||||||
|
* Date: 2016-03-15
|
||||||
|
* Time: 13:42
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
require('mocha-jshint')({paths:['md-validator.js']});
|
2
test/mocha.opts
Normal file
2
test/mocha.opts
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
--slow 200
|
||||||
|
--growl
|
Loading…
Reference in New Issue
Block a user