Some code tidying and optimisations

This commit is contained in:
Martin Donnelly 2016-03-17 11:59:23 +00:00
parent 7100cf5ed2
commit aaac571c81

View File

@ -15,7 +15,8 @@ VALIDATE = new function() {
}; };
/** /**
* Check to see if a date is actually a valid date (e.g. FF browser converts 31 Feb to an invalid date) * 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} d
* @param {number} m * @param {number} m
* @param {number} y * @param {number} y
@ -23,13 +24,9 @@ VALIDATE = new function() {
*/ */
this.isDateValid = function(d, m, y) { this.isDateValid = function(d, m, y) {
var enteredDate = new Date(y, m, d); var enteredDate = new Date(y, m, d);
/*
if (enteredDate.getFullYear() == y && enteredDate.getMonth() == m && enteredDate.getDate() == d) {
return true;
}
*/
return !!(enteredDate.getFullYear() === y && enteredDate.getMonth() === m && enteredDate.getDate() === d); return !!(enteredDate.getFullYear() === y && enteredDate.getMonth() === m &&
enteredDate.getDate() === d);
}; };
@ -39,18 +36,10 @@ VALIDATE = new function() {
*/ */
this.Email = function(email) { this.Email = function(email) {
var flag, b; var flag, b;
/*
if (/^\w+([\+\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
flag = true;
}
else
flag = false;
*/
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) { if (b.length > 2) {
flag = false; flag = false;
} }
@ -71,7 +60,8 @@ VALIDATE = new function() {
this.time = function(inval) { this.time = function(inval) {
var rtrn = false, exp = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/; var rtrn = false;
var exp = /^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$/;
if (exp.test(inval)) { if (exp.test(inval)) {
rtrn = true; rtrn = true;
@ -86,7 +76,8 @@ VALIDATE = new function() {
* @returns {boolean} * @returns {boolean}
*/ */
this.money = function(inval) { this.money = function(inval) {
var rtrn = '', exp = /^\xA3?\d{1,3}?([,]\d{3}|\d)*?([.]\d{1,2})?$/; var rtrn = '';
var exp = /^\xA3?\d{1,3}?([,]\d{3}|\d)*?([.]\d{1,2})?$/;
if (exp.test(inval)) { if (exp.test(inval)) {
rtrn = exp.exec(inval); rtrn = exp.exec(inval);
@ -118,16 +109,20 @@ VALIDATE = new function() {
var pcexp = []; var pcexp = [];
// BFPO postcodes // BFPO postcodes
pcexp.push(new RegExp('^(bf1)(\\s*)([0-6]{1}' + BFPOa5 + '{1}' + BFPOa6 + '{1})$', 'i')); 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 // 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')); pcexp.push(new RegExp('^(' + alpha1 + '{1}' + alpha2
+ '?[0-9]{1,2})(\\s*)([0-9]{1}' + alpha5 + '{2})$', 'i'));
// Expression for postcodes: ANA NAA // Expression for postcodes: ANA NAA
pcexp.push(new RegExp('^(' + alpha1 + '{1}[0-9]{1}' + alpha3 + '{1})(\\s*)([0-9]{1}' + alpha5 + '{2})$', 'i')); pcexp.push(new RegExp('^(' + alpha1 + '{1}[0-9]{1}' + alpha3
+ '{1})(\\s*)([0-9]{1}' + alpha5 + '{2})$', 'i'));
// Expression for postcodes: AANA NAA // 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')); 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 // Exception for the special postcode GIR 0AA
pcexp.push(/^(GIR)(\s*)(0AA)$/i); pcexp.push(/^(GIR)(\s*)(0AA)$/i);
@ -135,7 +130,7 @@ VALIDATE = new function() {
// Standard BFPO numbers // Standard BFPO numbers
pcexp.push(/^(bfpo)(\s*)([0-9]{1,4})$/i); pcexp.push(/^(bfpo)(\s*)([0-9]{1,4})$/i);
// c/o BFPO numbers // C/o BFPO numbers
pcexp.push(/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i); pcexp.push(/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
// Overseas Territories // Overseas Territories
@ -167,7 +162,9 @@ 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;
@ -183,11 +180,8 @@ VALIDATE = new function() {
// Return with either the reformatted valid postcode or the original // Return with either the reformatted valid postcode or the original
// invalid postcode // invalid postcode
//if (valid) {return postCode;} else return false;
if (valid) {return postCode;} return valid ? postCode : '';
else {
return '';
}
}; };
/** /**
@ -198,9 +192,8 @@ VALIDATE = new function() {
this.checkUKTelephone = function(telephoneNumber) { this.checkUKTelephone = function(telephoneNumber) {
// 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 telnum = telephoneNumber + ' ';
if (telnum.length === 1) { if (telnum.length === 1) {
// telNumberErrorNo = 1;
return ''; return '';
} }
telnum = (telnum).trim(); telnum = (telnum).trim();
@ -208,34 +201,31 @@ 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;
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);
} }
// 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;
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;
return ''; return '';
} }
@ -257,7 +247,6 @@ VALIDATE = new function() {
for (var i = 0; i < tnexp.length; i++) { for (var i = 0; i < tnexp.length; i++) {
if (tnexp[i].test(telnum)) { if (tnexp[i].test(telnum)) {
telNumberErrorNo = 5;
return ''; return '';
} }
} }
@ -265,7 +254,6 @@ 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;
return ''; return '';
} }
@ -279,6 +267,7 @@ VALIDATE = new function() {
* @returns {string} * @returns {string}
*/ */
this.validatePhone = function(params) { this.validatePhone = function(params) {
var onlyDigits;
var uk, v, required, itrx; var uk, v, required, itrx;
if (typeof params === 'object') { if (typeof params === 'object') {
uk = params.uk || false; uk = params.uk || false;
@ -292,11 +281,11 @@ VALIDATE = new function() {
required = false; required = false;
} }
onlyDigits = function(inval) {
var ch;
var onlyDigits = function(inval) { var ws = '';
var allowed = '0123456789+';
var ch, ws = '', allowed = "0123456789+";
for (var t = 0; t <= inval.length; t++) { for (var t = 0; t <= inval.length; t++) {
ch = inval.charAt(t); ch = inval.charAt(t);
@ -316,32 +305,31 @@ VALIDATE = new function() {
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) { if (v.length < 8) {
return ''; 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") { if (v.substr(0, 3) === '+44') {
v = "0" + v.substr(3, v.length); v = '0' + v.substr(3, v.length);
} }
if (v.substr(0, 4) === "0044") { if (v.substr(0, 4) === '0044') {
v = "0" + v.substr(4, v.length); v = '0' + v.substr(4, v.length);
} }
var pcv = this.checkUKTelephone(v); var pcv = this.checkUKTelephone(v);
if (pcv === false) { if (pcv === false) {
@ -350,12 +338,11 @@ VALIDATE = new function() {
if (pcv !== false) { if (pcv !== false) {
v = pcv; 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 '';
} }