How to compare 2 cells in validate method

Hello
Im trying to validate data cells by comparing eachother.
i have a grid with rows containing two columns (FROM-TIME, TO-TIME)
when i send data through onsubmit of button
function save() { myDataProcessor.sendData(); }
the validation process should compare from-time and to-time in order to validate that FROM-TIME < TO-TIME.
time should 24hrs format. ex: From-time(13:00),To-time(15:00).data must be submitted only when validation is true.
How to compare these 2 cells with help of custom validation.

thanks & regards
narinas

you can define validator as

dp.checkBeforeUpdate = function(rowid){
var vala = grid.cells(rowId,1);
var valb = grid.cells(rowId,2);
return some_validate_logic(vala, valb);
}

where some_validate_logic - you custom validation method

 function doInitGrid()
 {
    var fac_id=$("#cbo_fac_id").val();
	var cat_id=$("#cbo_cat_id").val();
	var cat_name=$("#cbo_cat_id :selected").text();//extracting text from category(used for displaying messages)
	$("#result").html("&nbsp;");
	mygrid = new dhtmlXGridObject('mygrid_content');
	mygrid.setImagePath("dhtml/dhtmlxgrid/codebase/imgs/");
	mygrid.setHeader(" TYPE,img:[images-load/clock.png]FROM_TIME,img:[images-load/clock.png]TO_TIME,EMP-RATE,OFF-RATE,STATUS,img:[images-load/calendar.gif]EFFECTIVE_DATE");
	mygrid.setInitWidths("90,110,100,100,100,80,125");
	mygrid.setColAlign("left,center,center,center,center,center,center");
	mygrid.setColSorting("str,str,str,na,na,na,na");
	mygrid.setColTypes("ro,ed,ed,ed,ed,ch,dhxCalendar");
	mygrid.setSkin("dhx_blue");
	mygrid.enableAlterCss("even","uneven");
	mygrid.init();
	mygrid.loadXML("load.php?fac_id="+fac_id+"&cat_id="+cat_id);
	myDataProcessor = new dataProcessor("update.php?fac_id="+fac_id+"&cat_id="+cat_id+"&cat_name="+cat_name); 
	myDataProcessor.setTransactionMode("POST",true); //set mode as send-all-by-post
	myDataProcessor.setUpdateMode("off"); //disable auto-update
	myDataProcessor.setVerificator(1,validate_grid);
	myDataProcessor.setVerificator(2,validate_grid);
	myDataProcessor.setVerificator(3,validate_grid);
	myDataProcessor.setVerificator(4,validate_grid);
	myDataProcessor.setVerificator(6,validate_grid);
	myDataProcessor.attachEvent("onRowMark", function(id) {
    if (this.is_invalid(id) == "invalid")
        return false;
    return true;
    });
	myDataProcessor.init(mygrid);//link dataprocessor to grid
    //Response:	
	myDataProcessor.defineAction('insert',function(response)
	{
		var msg=response.getAttribute("msg");
		$("#result").append("<br><font color='#484848'><b>"+msg+"</b></font>");
         return true;
	});
	myDataProcessor.defineAction('update',function(response)
	{
		var msg=response.getAttribute("msg");
		$("#result").append("<br><font color='#484848'><b>"+msg+"</b></font>");
	    return true;
	});

 }
//---------------------------------------[*validation code*]--------------------------------------------
var err_str="";
function validate_grid(value,id,ind)
	{
	  	if(ind==1 || ind==2)
		{
            var patt=/^([0][0-9]|[0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/;
			if(!patt.test(value))
			{
			    mygrid.setCellTextStyle(id,ind,"background-color:yellow;");
			  	if(err_str!="")
					err_str+="\n"+(ind+1)+".Enter numbers and (:) only..[ex.05:00 24hr format]";
				else
					err_str=(ind+1)+".Enter numbers and (:) only..[ex.05:00 24hr format]";
				return false;
			}
			else 
			{
				mygrid.setCellTextStyle(id,ind,"background-color:white;");
				return true;
			}
	    }
		else if(ind==3 || ind==4)
		{
			var patt=/^[0-9]+[\.]?[0-9]{0,2}$/;
			if(!patt.test(value))
			{
				mygrid.setCellTextStyle(id,ind,"background-color:yellow;");
				if(err_str!="")
					err_str+="\n"+(ind+1)+".Enter floating type values only..(eg:7.5)";
				else
					err_str=(ind+1)+".Enter floating type values only..(eg:7.5)";
				return false;
			}
			else
			{
				mygrid.setCellTextStyle(id,ind,"background-color:white;");
				return true;
			}
		}
		else if(ind==6)
		{
            var patt=/^[0-9]{2}[\-][a-z]{3}[\-][0-9]{4}$/i;
			if(!patt.test(value))
			{
				mygrid.setCellTextStyle(id,ind,"background-color:yellow;");
				if(err_str!="")
					err_str+="\n"+(ind+1)+".Please select date";
				else
					err_str=(ind+1)+".Select Date";
				return false;
			}
			else
			{
				mygrid.setCellTextStyle(id,ind,"background-color:white;");
				return true;
			}
		}
	}

function save()
	{
  	   myDataProcessor.sendData();//sending request.
	   if(err_str!="")
	   jAlert('info',err_str,'Error Message');
	   err_str="";
	   $("#result").html("&nbsp;");
	}
<input type="button" value="Save" id="btn_update" onClick="save()" ></input>

in javascript i have done like

 if(err_str=="")
	 {
	 var fromtime=document.myform.f_time.value;
	 var totime=document.myform.t_time.value;
	 var ft=fromtime.split(":");
	 var tt=totime.split(":");
	 var ok=false;
	 if(tt[0]*1 > ft[0]*1) {ok=true}
	 if((tt[0]*1 == ft[0]*1) && (tt[1]*1 > ft[1]*1)){ok=true}
	 if(ok)
		{
		  var hr=tt[0]-ft[0];
		  var mn=tt[1]-ft[1];
		  if(mn<0){mn=mn+60; hr=hr-1}
		  if(mn<10){mn="0"+mn}
		  //alert("total hours:"+hr+":"+mn);
		}
	 else
		{
		  if(err_str=="")
		  err_str="TO_TIME should be greater than FROM_TIME.";
		}

please show me how to do this custom validation in this grid page.

thanks
narinas.

http://forum.dhtmlx.com/viewtopic.php?f=2&t=12770

i have to loop this validation,im having 4 rows in this grid it should check for every row.

i have to loop this validation,im having 4 rows in this grid it should check for every row.

checkBeforeUpdate will be called for each row which is marked as updated

where should i write this part of code and how to use checkBeforeUpdate method,im new to this concept.please suggest me.can u provide me an example for this.

Each time when row sent to server side, dataprocessor calls checkBeforeUpdate method. If you need to have a complex validation rules you can redefine that method with your custom logic.

You can place it as a part of dataporocessor initialization

var dp = new dataProcessor(); dp.init(mygrid); dp.checkBeforeUpdate = function(rowId){ var vala = grid.cells(rowId,1); var valb = grid.cells(rowId,2); return some_validate_logic(vala, valb); };

1,2 - index of columns which you want to compare
some_validate_logic - name of method which you need to write, it will receive two values and must return true if they are valid, or false otherwise

thank you.

Hi Stanislav,
I have used the method mygrid.forEachRow(function(id){ }); for comparing time in grid.

function compare_times()
{	
	mygrid.forEachRow(function(id)
	{
	    var val1=mygrid.cells(id,1).getValue();
  	    var val2=mygrid.cells(id,2).getValue();
			var ft=val1.split(":");
			var tt=val2.split(":");
			var ok=false;
			if(tt[0]*1 > ft[0]*1) 
			{
			ok=true;
			return ok;
			}
			else if((tt[0]*1 == ft[0]*1) && (tt[1]*1 > ft[1]*1))
			{
			ok=true
			return ok;
			}
			else
			{
			if(val1!="" && val2!="")
			{
			 if(err_str!="")
					err_str+="\n"+(id)+".TO_TIME should be greater than FROM_TIME";
				else
					err_str=(id)+".TO_TIME should be greater than FROM_TIME";
			}
			return ok;
			}
        return false;
	});
}

compare_times() function is working fine problem is with save() function.
now dataprocessor should call only after comparision between this 2 cells is true.
i have written it as

function save() { if(compare_times()) { myDataProcessor.sendData();//sending request. } if(err_str!="") jAlert('info',err_str,'Error Message'); err_str=""; $("#result").html("&nbsp;"); }
on sumbit i.e; function save();If compare_times() is true myDataProcessor.sendData(); should work,but this is not working,can u tell me where i went wrong.

You compare_times method do not return any value actually, it need to be changed as

function compare_times(){ var result = true; mygrid.forEachRow(function(id) { var val1=mygrid.cells(id,1).getValue(); var val2=mygrid.cells(id,2).getValue(); var ft=val1.split(":"); var tt=val2.split(":"); var ok=false; if(tt[0]*1 > ft[0]*1) { ok=true; return ok; } else if((tt[0]*1 == ft[0]*1) && (tt[1]*1 > ft[1]*1)) { ok=true return ok; } else { if(val1!="" && val2!="") { if(err_str!="") err_str+="\n"+(id)+".TO_TIME should be greater than FROM_TIME"; else err_str=(id)+".TO_TIME should be greater than FROM_TIME"; } return ok; } result = false; /*set return flag*/ }); return result; //return flag }

i want to highlight the cell which has error while checking compare_times() for that i need to use “ind” but when im using im getting error.

In dataprocessor validation:
myDataProcessor.setVerificator(1,validate_grid);
myDataProcessor.setVerificator(2,validate_grid);
in this method i can display error by highlighting the cell with yellow color.

var err_str="";
function validate_grid(value,id,ind)
{
  if(ind==1 || ind==2)
   {
     var patt=/^([0][0-9]|[0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/;
     if(!patt.test(value))
	{
	    mygrid.setCellTextStyle(id,ind,"background-color:yellow;");
    	    if(err_str!="")
		err_str+="\n"+(ind+1)+".Enter numbers and (:) only..[ex.05:00 24hr format]";
	    else
		err_str=(ind+1)+".Enter numbers and (:) only..[ex.05:00 24hr format]";
	   return false;
	}
	else 
	{
	   mygrid.setCellTextStyle(id,ind,"background-color:white;");
	   return true;
	}
 }

can i include “ind” in
mygrid.forEachRow(function(id){});
as
mygrid.forEachRow(function(id,ind){ });//this is not working.

Hi Stanislav,

I got the answer,i have checked the comparision in the dataprocessor only.
myDataProcessor.setVerificator(1,validate_grid);
myDataProcessor.setVerificator(2,validate_grid);

var err_str="";
function validate_grid(value,id,ind)
{
  if(ind==1 || ind==2)
   {
     var patt=/^([0][0-9]|[0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/;
      if(!patt.test(value))
	{
	   mygrid.setCellTextStyle(id,ind,"background-color:yellow;");
	   if(err_str!="")
	       err_str+="\n"+(ind+1)+".Enter numbers and (:) only..[ex.05:00 24hr format]";
	   else
		err_str=(ind+1)+".Enter numbers and (:) only..[ex.05:00 24hr format]";
	  return false;
	}
	else 
	{   //COMPARING TIME BETWEEN FROM-TIME & TO-TIME.
	 var val1=mygrid.cells(id,1).getValue();
	 var val2=mygrid.cells(id,2).getValue();
	 var ft=val1.split(":");
	 var tt=val2.split(":");
	 if(tt[0]*1 > ft[0]*1)
	 {
		mygrid.setCellTextStyle(id,ind,"background-color:white;");
		return true;
	 }
	else if((tt[0]*1 == ft[0]*1) && (tt[1]*1 > ft[1]*1))
	 {
	      mygrid.setCellTextStyle(id,ind,"background-color:white;");
		return true;
	 }
	 else
	 {  
	     mygrid.setCellTextStyle(id,ind,"background-color:Yellow;");
	     if(err_str!="")
	         err_str+="\n"+(ind+1)+".TO_TIME should be greater than FROM_TIME";
	      else
		  err_str=(ind+1)+".TO_TIME should be greater than FROM_TIME";
	      return false;
	 }
    }
}

and i have a doubt when the comparision b/w times is false im alerting message,im getting the message twice for both the “ind”-1,2
ex:for (id-1) ind-1 is true and ind-2 is false,i should get only one error message for(ind-2) but message is comming twice. any idea.

thank you for ur support.

I think you need not to assign verification two times

myDataProcessor.setVerificator(1,validate_grid);
myDataProcessor.setVerificator(2,validate_grid);

instead you can use

myDataProcessor.setVerificator(1,validate_grid);

When you are updating any cell in the row - grid will run all verificators ( for all columns ) in that row, so having only one will work for changes in both cells.

how to get the name from its “id”
say id of row 1 has a name stock in my db, how to retreive the name from the id,so that i can use it in the validation error message.
To-time should be greater than From-time for (stock)->following id.

hey stanislav,

i got it .
var val0=mygrid.cells(id,0).getValue(); gives the value of id.

thanks a lot for ur support.

Hey Stanislav,

Thanks for ur inputs,
could also throw some light on how do i convert the following code (which is more of C syntax) into grids

for(i=0;i<3;i++){
int k=From_time[i] //value of cell named from_time of row 'i'
  for(j=i+1;j<4;j++){
    int l =To_time[j] //value of cell named to_time of row 'j'
     if(l>k) success
     else fail
  }
}

Basically i need to compare column 2 of row 1 with column 1 of row 2 and like viz… i.e. Assuming rows are in ascending order, column 2 value of any row should be less than column 1 value of next row.
Example:
Row 1 5:00 6:00
Row 2 7:00 8:00
Success Condition
but if
Row 1 5:00 6:00
Row 2 5:45 8:00 --Should Fail

int k=From_time[i]
var k = grid.cells2(i, from_index).getValue();

above code assumes that i is a row index ( not row id )
from_index - index of column with from_time in grid
If you are using columnids - it can be defined as
var from_time = grid.getColIndexById(“from_index”);

function compare_row_time()
{
var status_flag =false;
 for (var i=0; i<3; i++)
 { //change limiting condition to dynamic value,currently it is 3
  var tt_row=mygrid.cells(mygrid.getRowId(i),2).getvalue(); 
  var tt_split=tt_row.split(":");		   
   for (var j=i+1; j<4; j++)
   {
    var ft_row=mygrid.cells(mygrid.getRowId(j),1).getvalue();
    var ft_split=ft_row.split(":");
    if(ft_split[0]*1 > tt_split[0]*1)
      {
	mygrid.setCellTextStyle(mygrid.getRowId(j),1,"background-color:white;");
       }
	else if((ft_split[0]*1 == tt_split[0]*1) && (ft_split[1]*1 > tt_split[1]*1))
	{
    	 mygrid.setCellTextStyle(mygrid.getRowId(j),1,"background-color:white;");
 	}
	else
	{
	mygrid.setCellTextStyle(mygrid.getRowId(j),1,"background-color:yellow;");
	status_flag = true;
	}
    }
    }
return status_flag;
}

in function save()

function save()
	{
	 if(!compare_row_time){
	 myDataProcessor.sendData();
	 }
	 if(err_str!="")
	alert(err_str);
	 err_str="";
	  $("#result").html("&nbsp;");
	}