mirror of
https://gitlab.silvrtree.co.uk/martind2000/node-validator.git
synced 2025-01-25 16:16:16 +00:00
Some code tidying and optimisations
This commit is contained in:
parent
7100cf5ed2
commit
aaac571c81
@ -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} m
|
||||
* @param {number} y
|
||||
@ -23,13 +24,9 @@ VALIDATE = new function() {
|
||||
*/
|
||||
this.isDateValid = function(d, m, y) {
|
||||
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) {
|
||||
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);
|
||||
|
||||
b = email.split("@");
|
||||
b = email.split('@');
|
||||
if (b.length > 2) {
|
||||
flag = false;
|
||||
}
|
||||
@ -71,7 +60,8 @@ VALIDATE = new function() {
|
||||
|
||||
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)) {
|
||||
rtrn = true;
|
||||
@ -86,7 +76,8 @@ VALIDATE = new function() {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
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)) {
|
||||
rtrn = exp.exec(inval);
|
||||
@ -118,16 +109,20 @@ VALIDATE = new function() {
|
||||
var pcexp = [];
|
||||
|
||||
// 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
|
||||
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
|
||||
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
|
||||
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
|
||||
pcexp.push(/^(GIR)(\s*)(0AA)$/i);
|
||||
@ -135,7 +130,7 @@ VALIDATE = new function() {
|
||||
// Standard BFPO numbers
|
||||
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);
|
||||
|
||||
// Overseas Territories
|
||||
@ -167,7 +162,9 @@ VALIDATE = new function() {
|
||||
|
||||
// If it is the Anguilla overseas territory postcode, we need to
|
||||
// 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
|
||||
valid = true;
|
||||
|
||||
@ -183,11 +180,8 @@ VALIDATE = new function() {
|
||||
|
||||
// Return with either the reformatted valid postcode or the original
|
||||
// invalid postcode
|
||||
//if (valid) {return postCode;} else return false;
|
||||
if (valid) {return postCode;}
|
||||
else {
|
||||
return '';
|
||||
}
|
||||
|
||||
return valid ? postCode : '';
|
||||
};
|
||||
|
||||
/**
|
||||
@ -198,9 +192,8 @@ VALIDATE = new function() {
|
||||
this.checkUKTelephone = function(telephoneNumber) {
|
||||
|
||||
// Convert into a string and check that we were provided with something
|
||||
var telNumberErrorNo, telnum = telephoneNumber + ' ';
|
||||
var telnum = telephoneNumber + ' ';
|
||||
if (telnum.length === 1) {
|
||||
// telNumberErrorNo = 1;
|
||||
return '';
|
||||
}
|
||||
telnum = (telnum).trim();
|
||||
@ -208,34 +201,31 @@ VALIDATE = new function() {
|
||||
// Don't allow country codes to be included (assumes a leading "+")
|
||||
var exp = /^(\+)[\s]*(.*)$/;
|
||||
if (exp.test(telnum) === true) {
|
||||
//telNumberErrorNo = 2;
|
||||
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);
|
||||
}
|
||||
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
|
||||
|
||||
exp = /^[0-9]{10,11}$/;
|
||||
if (exp.test(telnum) !== true) {
|
||||
// telNumberErrorNo = 3;
|
||||
return '';
|
||||
}
|
||||
|
||||
// Now check that the first digit is 0
|
||||
exp = /^0[0-9]{9,10}$/;
|
||||
if (exp.test(telnum) !== true) {
|
||||
// telNumberErrorNo = 4;
|
||||
return '';
|
||||
}
|
||||
|
||||
@ -257,7 +247,6 @@ VALIDATE = new function() {
|
||||
|
||||
for (var i = 0; i < tnexp.length; i++) {
|
||||
if (tnexp[i].test(telnum)) {
|
||||
telNumberErrorNo = 5;
|
||||
return '';
|
||||
}
|
||||
}
|
||||
@ -265,7 +254,6 @@ VALIDATE = new function() {
|
||||
// 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) {
|
||||
// telNumberErrorNo = 5;
|
||||
return '';
|
||||
}
|
||||
|
||||
@ -279,6 +267,7 @@ VALIDATE = new function() {
|
||||
* @returns {string}
|
||||
*/
|
||||
this.validatePhone = function(params) {
|
||||
var onlyDigits;
|
||||
var uk, v, required, itrx;
|
||||
if (typeof params === 'object') {
|
||||
uk = params.uk || false;
|
||||
@ -292,11 +281,11 @@ VALIDATE = new function() {
|
||||
required = false;
|
||||
}
|
||||
|
||||
onlyDigits = function(inval) {
|
||||
|
||||
|
||||
var onlyDigits = function(inval) {
|
||||
|
||||
var ch, ws = '', allowed = "0123456789+";
|
||||
var ch;
|
||||
var ws = '';
|
||||
var allowed = '0123456789+';
|
||||
|
||||
for (var t = 0; t <= inval.length; 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 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) {
|
||||
itrx = internationalTest1.exec(v);
|
||||
v = "00" + itrx[3] + itrx[5];
|
||||
}
|
||||
else if (internationalTest2.test(v) === true) {
|
||||
v = '00' + itrx[3] + itrx[5];
|
||||
} else if (internationalTest2.test(v) === true) {
|
||||
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);
|
||||
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) {
|
||||
// perform uk specific tests
|
||||
// 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, 3) === '+44') {
|
||||
v = '0' + v.substr(3, v.length);
|
||||
}
|
||||
if (v.substr(0, 4) === "0044") {
|
||||
v = "0" + v.substr(4, v.length);
|
||||
if (v.substr(0, 4) === '0044') {
|
||||
v = '0' + v.substr(4, v.length);
|
||||
}
|
||||
var pcv = this.checkUKTelephone(v);
|
||||
if (pcv === false) {
|
||||
@ -350,12 +338,11 @@ VALIDATE = new function() {
|
||||
if (pcv !== false) {
|
||||
v = pcv;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// assume foreign
|
||||
} else {
|
||||
// Assume foreign
|
||||
|
||||
if (!(v.charAt(0) === "+" || v.substr(0, 2) === "00")) {
|
||||
// not right?
|
||||
if (!(v.charAt(0) === '+' || v.substr(0, 2) === '00')) {
|
||||
// Not right?
|
||||
return '';
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user