Sunday, December 23, 2007

range validation in javascript

function rangeObject(minValue,maxValue){
this.minValue = minValue;
this.maxValue = maxValue;
this.getMinValue = function(){
return this.minValue;
}
this.getMaxValue = function(){
return this.maxValue;
}

this.validate = function(){
t_str_min = new String(this.minValue);
t_str_max = new String(this.maxValue);
t_str_min = t_str_min.replace(' ','');
t_str_max = t_str_max.replace(' ','');
if(t_str_min.length==0 ||
t_str_max.length==0){
return false;

}else if(isNaN(this.minValue) || isNaN(this.maxValue)){
return false;
}else{
this.minValue = parseInt(this.minValue);
this.maxValue = parseInt(this.maxValue);
if(this.minValue>this.maxValue){
return false;
}else{
return true;
}
}
}
this.isOverlaped = function(tRangeObject){
var secondMinValue = tRangeObject.getMinValue();
var secondMaxValue = tRangeObject.getMaxValue();
if((secondMinValue <= this.minValue &&
this.minValue <= secondMaxValue)
|| (secondMinValue <= this.maxValue &&
this.maxValue <= secondMaxValue)
|| (secondMinValue <= this.maxValue &&
this.maxValue <= secondMaxValue)
|| (this.minValue<=secondMinValue &&
this.maxValue >=secondMaxValue )){
return true;
}else{
return false;
}
}
this.getValues = function(){
return this.minValue + ' : ' + this.maxValue;
}

}
function checkOverlap(noOfRows){
var rangeObjectArray = new Array();
for(i=1;i<=noOfRows;i++){
var t_min = eval('document.theForm.' + 'rMIN_' + i).value;
var t_max = eval('document.theForm.' + 'rMAX_' + i).value;
var tempRangeObject = new rangeObject(t_min,t_max);
rangeObjectArray.push(tempRangeObject);
}
for(j=0;j < rangeObjectArray.length;j++){
var t_validStr='';
if(!rangeObjectArray[j].validate()){
t_validStr = t_validStr + 'values entered in row ' + (j+1) + ' not valid \n';
}
if(t_validStr!=''){
alert(t_validStr);
return false;
}
}
for(j=0;j < rangeObjectArray.length;j++){
for(k=j+1;k < rangeObjectArray.length;k++){
if(rangeObjectArray[j].isOverlaped(rangeObjectArray[k])){
alert(rangeObjectArray[j].getValues() + ' ::: ' + rangeObjectArray[k].getValues() + ' are overlapped');
return false;
}
}
}
return true;
}

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]