I am doing a date validation. I have no. of pages, which have the date field. User can input the date like "220875" or "22AUG75" - I need to test both and check the length as well.
function isValidDate(newDate) {
newDate[1] = newDate[1]-1;
newDate[2] = (parseInt(newDate[2]) < 50) ? 2000 + parseInt(newDate[2]) : 1900 + parseInt(newDate[2]);
var testDate = new Date(newDate[2], newDate[1], newDate[0]);
if (testDate.getDate()!=newDate[0] || testDate.getMonth()!=newDate[1] || testDate.getFullYear()!=newDate[2]) {
return false;
} else {
return {valid : true, date : testDate};
}
}
var sandBox = {
init:function(params){
this.element = $(params.element),
this.value = params.value,
this.mode = params.num;
},
isNotEmpty:function(){
if(!this.value.length) {
this.errorHandler(this.emptyMsg);
} else {
this.errorHandler(true);
return true;
}
},
errorHandler: function (msg) {
var errorHolder = this.element.siblings('span');
if(msg !== true)
errorHolder.html(msg).show();
else errorHolder.html("").hide();
},
emptyMsg : "Date field should not be empty",
digitLengthMsg : "The Length of the value should be ",
onlyNumber : {
num : /\D/,
str : /^\d{2}[a-zA-Z]{3}\d{2}$/,
mon : /[a-zA-Z]{3}/,
msg : "Only Digits allowed"
},
getMonthFromString : function(mon){
var month = new Date(Date.parse(mon +" 01, 12")).getMonth()+1;
month = month < 10 ? '0'+ month : month;
return month;
},
isValidData : function () {
var result = this.onlyNumber.str.test(this.value);
if(this.mode && result) {
this.errorHandler(this.onlyNumber.msg)
return false;
}
else if (!this.mode && result) {
var currentMonth = this.getMonthFromString(this.value.match(this.onlyNumber.mon)[0]);
this.value = this.value.replace(/([a-zA-Z]){3}/g, currentMonth);
return true;
}
else if (this.mode && this.value.length !== 6) {
this.errorHandler(this.digitLengthMsg + '6');
return false;
}
else if (!this.mode && this.value.length !== 7) {
this.errorHandler(this.digitLengthMsg + '7');
return false;
}
else {
this.errorHandler(true);
return true;
}
},
DateIsOk:function() {
if(this.isNotEmpty() && this.isValidData()) {
var verifyDate = this.value.match(/.{1,2}/g);
return isValidDate(verifyDate);
}
}
};
var dateValidator = function (e) {
var value = e.target.value;
sandBox.init({
value : value,
element : e.target,
num : true //set month status true means validation by digits, false means digits with string
});
var fieldValue = sandBox.DateIsOk();
if(fieldValue) {
console.log(fieldValue);
}
};
var dateField = $('input');
dateField.on('keyup focusout', dateValidator);
this.mode
represents. I assume it's a toggle between validating220875
and22AUG75
. You should probably rename the variable. If for some reason it is imperative that it stay named like that, please document its behaviour. Also,this.value.match(/.{1,2}/g)
doesn't seem intuitive at all. What exactly do you want to achieve by doing this? \$\endgroup\$User can input the date like "220875" or "22AUG75"
-- why these formats rather than a standard format that people are already familiar with andDate
will recognize? \$\endgroup\$