Tuesday, June 3, 2008

Cruxade's Data validation library

I am beginning to create a data validation library under Cruxade. I'll be happy to have your contribution to the code and make it better.

function isBlank(c){
if (null==c) return true;
return c.search(/^\s*$/)!=-1;
}

function trimSpaces(c){
if (null==c) return "";
if (isBlank(c)) return "";
c=c.replace(/^\s+/,"");
c=c.replace(/\s+$/,"");
return c;
}

function isDigit(n){
if (null==n) return false;
if (typeof n == "number") return true;
n = trimSpaces(n);
return n.search(/^\d+\.*\d*$/)!=-1;
}

function isDate(d){
if (null==d) return false;
if (d instanceof Date) return true;
d = trimSpaces(d);
if (d.search(/^\d+-\d+-\d+$/)==-1) return false;
var lv_nYear = parseInt(d.match(/^\d+/));
var lv_cMonth = String(d.match(/-\d+-/));
lv_nMonth = parseInt(lv_cMonth.match(/\d+/));
lv_cDay = String(d.match(/-\d+$/));
lv_nDay = parseInt(lv_cDay.match(/\d+/));

// check for leap year and if day corresponds to month
if (lv_nYear < 1) return false;
if ((lv_nMonth < 1 ) || (lv_nMonth > 12)) return false
if ((lv_nDay < 1) || (lv_nDay > 31)) return false;
if (lv_nMonth==2){
if (lv_nDay<=28) return true;
if (lv_nDay>29) return false;
var lv_nScore= 0;
lv_nScore = (lv_nYear % 400==0)?1:0;
if (lv_nScore==0){
lv_nScore = (lv_nYear % 100==0)?-100:0;
lv_nScore = lv_nScore + ((lv_nYear % 4 ==0)?3:0);
}
return lv_nScore > 0;
}
if (lv_nDay==31){
var lv_c30month={};
lv_c30month[4]="";
lv_c30month[6]="";
lv_c30month[9]="";
lv_c30month[11]="";
return !(lv_nMonth in lv_c30month);
}
return true;
}
:-)

edited: 2008-06-06

1 comment:

feathervane said...

Loved the treatment of gplv3 icon... Also, the step-by-step process on adding a patient was indeed a good feature.