validating time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • narinas
    New Member
    • Jan 2012
    • 20

    validating time

    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;
    			}
    		}
    }
    here ind==1 is from-time and ind==2 is to-time,from-time should not be greater than to-time and to-time should not be less than from-time
    ex:05:00-09:00(true)
    09:00-08:00(false).
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    One way to do this is to use the Date object:
    1. Create 2 dates using the from/to times
    2. Compare them

    Comment

    • narinas
      New Member
      • Jan 2012
      • 20

      #3
      can u show me how to do that using the above format.

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        As I mentioned, it's one way. See below if you want to use dates.

        Another method is to split on the ":" to get the hours and minutes and then compare the hours. If equal, compare the minutes. That should be straightforward . If not, post your attempt.

        If using the Date method, see https://developer.mozilla.org/en/Jav...l_Objects/Date for info. on Dates.
        It has enough to get you started on creating a Date (split on ":" and create using either new or set** methods.

        To compare, use the comparison operator (e.g. < or >) on 2 Date objects.

        Comment

        Working...