node-validator/lib/md-validator.js

359 lines
9.0 KiB
JavaScript

'use strict';
/**
*
* User: Martin Donnelly
* Date: 2016-03-09
* Time: 14:26
*
*/
/* exported VALIDATE */
var VALIDATE;
VALIDATE = new function() {
this.dateBuilder = function(d, m, y) {
return new Date(Date.UTC(y, m, d, 0, 0, 0));
};
/**
* Check to see if a date is actually a valid date (e.g. FF browser converts
* 31 Feb to an invalid date)
* @param {number} d
* @param {number} m
* @param {number} y
* @returns {boolean}
*/
this.isDateValid = function(d, m, y) {
var enteredDate = new Date(y, m, d);
return !!(enteredDate.getFullYear() === y && enteredDate.getMonth() === m &&
enteredDate.getDate() === d);
};
/**
* Validates emails
* @return {boolean}
*/
this.Email = function(email) {
var flag, b;
flag = !!/^\w+([\+\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email);
b = email.split('@');
if (b.length > 2) {
flag = false;
}
if (b[0].length === 0) {
flag = false;
}
if (email.charAt(0) === '.' || email.charAt(email.length - 1) === '.') {
flag = false;
}
return flag;
};
/**
*
* @param inval
* @returns {boolean}
*/
this.time = function(inval) {
var rtrn = false;
var exp = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
if (exp.test(inval)) {
rtrn = true;
}
return rtrn;
};
/**
*
* @param inval
* @returns {boolean}
*/
this.money = function(inval) {
var rtrn = '';
var exp = /^\xA3?\d{1,3}?([,]\d{3}|\d)*?([.]\d{1,2})?$/;
if (exp.test(inval)) {
rtrn = exp.exec(inval);
}
return rtrn[0];
};
/**
* Validates UK postcodes
*/
this.checkPostCode = function(toCheck) {
// Permitted letters depend upon their position in the postcode.
var valid, postCode;
var alpha1 = '[abcdefghijklmnoprstuwyz]'; // Character 1
var alpha2 = '[abcdefghklmnopqrstuvwxy]'; // Character 2
var alpha3 = '[abcdefghjkpmnrstuvwxy]'; // Character 3
var alpha4 = '[abehmnprvwxy]'; // Character 4
var alpha5 = '[abdefghjlnpqrstuwxyz]'; // Character 5
var BFPOa5 = '[abdefghjlnpqrst]'; // BFPO alpha5
var BFPOa6 = '[abdefghjlnpqrstuwzyz]'; // BFPO alpha6
var invalidW1 = /^(W1)([eilmnopqrvwxyz]{1})(\s*)([0-9]{1}[abdefghjlnpqrstuwxyz]{2})$/i;
// Array holds the regular expressions for the valid postcodes
var pcexp = [];
// BFPO postcodes
pcexp.push(new RegExp('^(bf1)(\\s*)([0-6]{1}' + BFPOa5 + '{1}' + BFPOa6 + '{1})$', 'i'));
// Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
pcexp.push(new RegExp('^(' + alpha1 + '{1}' + alpha2 + '?[0-9]{1,2})(\\s*)([0-9]{1}' + alpha5 + '{2})$', 'i'));
// Expression for postcodes: ANA NAA
pcexp.push(new RegExp('^(' + alpha1 + '{1}[0-9]{1}' + alpha3 + '{1})(\\s*)([0-9]{1}' + alpha5 + '{2})$', 'i'));
// Expression for postcodes: AANA NAA
pcexp.push(new RegExp('^(' + alpha1 + '{1}' + alpha2 + '{1}' + '?[0-9]{1}' + alpha4 + '{1})(\\s*)([0-9]{1}' + alpha5 + '{2})$', 'i'));
// Exception for the special postcode GIR 0AA
pcexp.push(/^(GIR)(\s*)(0AA)$/i);
// Standard BFPO numbers
pcexp.push(/^(bfpo)(\s*)([0-9]{1,4})$/i);
// C/o BFPO numbers
pcexp.push(/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
// Overseas Territories
pcexp.push(/^([A-Z]{4})(\s*)(1ZZ)$/i);
// Anguilla
pcexp.push(/^(ai-2640)$/i);
// Load up the string to check
postCode = toCheck;
// Assume we're not going to find a valid postcode
valid = false;
// Check the string against the types of post codes
for (var i = 0; i < pcexp.length; i++) {
if (pcexp[i].test(postCode)) {
// The post code is valid - split the post code into component parts
pcexp[i].exec(postCode);
// Copy it back into the original string, converting it to uppercase
// and inserting a space between the inward and outward codes
postCode = RegExp.$1.toUpperCase() + ' ' + RegExp.$3.toUpperCase();
// If it is a BFPO c/o type postcode, tidy up the "c/o" part
postCode = postCode.replace(/C\/O\s*/, 'c/o ');
// If it is the Anguilla overseas territory postcode, we need to
// treat it specially
if (toCheck.toUpperCase() === 'AI-2640') {
postCode = 'AI-2640';
}
// Load new postcode back into the form element
valid = true;
// Remember that we have found that the code is valid and break
// from loop
break;
}
}
if (invalidW1.test(postCode)) {
valid = false;
}
// Return with either the reformatted valid postcode or the original
// invalid postcode
return valid ? postCode : '';
};
/**
* Validates UK Telephone numbers
* @param {string} telephoneNumber
* @returns {*}
*/
this.checkUKTelephone = function(telephoneNumber) {
// Convert into a string and check that we were provided with something
var telnum = telephoneNumber + ' ';
if (telnum.length === 1) {
return '';
}
telnum = (telnum).trim();
// Don't allow country codes to be included (assumes a leading "+")
var exp = /^(\+)[\s]*(.*)$/;
if (exp.test(telnum) === true) {
return '';
}
// Remove spaces from the telephone number to help validation
while (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);
}
// Now check that all the characters are digits
exp = /^[0-9]{10,11}$/;
if (exp.test(telnum) !== true) {
return '';
}
// Now check that the first digit is 0
exp = /^0[0-9]{9,10}$/;
if (exp.test(telnum) !== true) {
return '';
}
// Disallow numbers allocated for dramas.
// Array holds the regular expressions for the drama telephone numbers
var tnexp = [];
tnexp.push(
/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
tnexp.push(/^02079460[0-9]{3}$/);
tnexp.push(/^01914980[0-9]{3}$/);
tnexp.push(/^02890180[0-9]{3}$/);
tnexp.push(/^02920180[0-9]{3}$/);
tnexp.push(/^01632960[0-9]{3}$/);
tnexp.push(/^07700900[0-9]{3}$/);
tnexp.push(/^08081570[0-9]{3}$/);
tnexp.push(/^09098790[0-9]{3}$/);
tnexp.push(/^03069990[0-9]{3}$/);
for (var i = 0; i < tnexp.length; i++) {
if (tnexp[i].test(telnum)) {
return '';
}
}
// 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]+$/);
if (exp.test(telnum) !== true) {
return '';
}
// Telephone number seems to be valid - return the stripped telehone number
return telnum;
};
/**
*
* @param params
* @returns {string}
*/
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;
required = params.req || false;
}
if (typeof params === 'string') {
uk = true;
v = params;
required = false;
}
onlyDigits = function(inval) {
var ch;
var ws = '';
var allowed = '0123456789+';
for (var t = 0; t <= inval.length; t++) {
ch = inval.charAt(t);
if (allowed.indexOf(ch) !== -1) {
ws = ws + ch;
}
}
return ws;
};
if (v.length === 0 && required === true) {
return '';
}
if (v.length > 0) {
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;
// Tidy up international format numbers.
if (internationalTest1.test(v) === true) {
itrx = internationalTest1.exec(v);
v = '00' + itrx[3] + itrx[5];
} else if (internationalTest2.test(v) === true) {
itrx = internationalTest2.exec(v);
v = '00' + itrx[3] + itrx[4];
}
// Strip invalid characters
v = onlyDigits(v);
if (v.length < 8) {
return '';
}
// Only do this if it hasn't already failed
if (uk) {
// Perform uk specific tests
// de-internationalise the number
if (v.substr(0, 3) === '+44') {
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);
if (pcv === false) {
return '';
}
if (pcv !== false) {
v = pcv;
}
} else {
// Assume foreign
if (!(v.charAt(0) === '+' || v.substr(0, 2) === '00')) {
// Not right?
return '';
}
}
}
return v;
};
};
module.exports = VALIDATE;