Monday, July 20, 2009

JavaScript code to check either the given range is overlapping or not

<!--

Here is a small javascript code to check either the given range is overlapping or not.

Suppose there is requirment that user is defining a range and you have to validate either the range defined

by the user is overrlapping with the previously provided ranges, then this javascript validation code is for you !

-->

<html>

<head>

<script language="javascript">

function checkRangeOverlapping(x,y){

  var range= new Array();


  // The following section is the previous ranges

  range[range.length]= {'A':'0', 'B':'25'};

  range[range.length]= {'A':'51', 'B':'75'};

  range[range.length]= {'A':'26', 'B':'50'};

  range[range.length]= {'A':'76', 'B':'100'};

  ////////////////////////////////////////////////////////////////////


  // first figuring out that the given range is overlapping with how many previous ranges

  var overlappingWithRanges= new Array();

  for(var i=0; i<range.length; i++){

    if(isInBetween(x, range[i].A, range[i].B) || isInBetween(y, range[i].A, range[i].B)){

      overlappingWithRanges[overlappingWithRanges.length]= range[i];

    }else if(isInBetween(range[i].A, x, y) || isInBetween(range[i].B, x, y)){

      overlappingWithRanges[overlappingWithRanges.length]= range[i];

    }

  } // for

  /////////////////////////////////////////////////////////////////////////////////////////


  // Now for telling user that the given range is overlapping with how many ranges

  var errorMessage= "Given Range is overlapping with ";

  var isOverlapping= false;

  for(var i=0; i<overlappingWithRanges.length; i++){

    isOverlapping= true;

    errorMessage += overlappingWithRanges[i].A +"-" +overlappingWithRanges[i].B;

    if(i != overlappingWithRanges.length-1)

     errorMessage += " and "

  } // for

  /////////////////////////////////////////////////////////////////////////////////////////


  if(isOverlapping)

    alert(errorMessage);

} // function checkRangeOverlapping


function isInBetween(x, a, b){

  if(x >= a && x <=b)

    return true;

  else

    return false;

} // function isInBetween(number, number, number)


</script>

</head>

<body onload="checkRangeOverlapping(4,80)"></body>

</html>

3 comments:

JavaScriptBank.com said...

very cool & good tip, thank you very much for sharing.

Can you share this snippet on my JavaScript library?


Thank

sabita said...

Hi,
Really a nice code thnku but i have one more requirement ie.if at all the ranges are not overlapping we should add that to the given ranges and should display if any ranges are missing
ex: if we already have ranges 1 to 10 and if we r adding a new range 15 to 20 it should diply mess saying 10 to 15 range is missing

Huzefa said...

function checkRangeOverlapping(x,y){

// all the code as above .....


// Nows lets check either the given range is in continue with previous one or not
var rangeIsMissing= true;
for(var i=0; i<range.length; i++){
var differnceDetail= new DifferenceDetail(range[i], x);
if(differnceDetail.getDifference() == 1){
rangeIsMissing= false;
break;
} // if
} // for

if(rangeIsMissing)
alert("Some Range is missing before the specified range");

} // function checkRangeOverlapping

function DifferenceDetail(range, x){
var difference= x-range.B;
var range= range;
var upperLimit= x

this.getDifference= function(){
return difference;
};
}