Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to validate time where to-time must be greater than from-time.

    hi im using dhtml xgrid where i need to do validation for time ,(from-time and to-time i.e ind==1 and ind==2 ) to-time must be greater than from-time else i need to alert a message to-time must be greater than from-time.
    can any one send me the code for it time format is[05:00].
    Code:
    var err_str="";
    function validate_grid(value,id,ind)
    	{
    	$("#result").html(" ").show();
    		if(ind==1 || ind==2)
    		{
    			var patt=/^([0][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..";
    				else
    					err_str=(ind+1)+".Enter numbers and : only";
    				return false;
    			}
    			else
    			{
    				mygrid.setCellTextStyle(id,ind,"background-color:white;");
    				return true;
    			}
    		}
    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Double-posted How to validate time where to-time must be greater than from-time.

    Please look at forum rule #1.2


    Code:
    <script type = "text/javascript">
    
    var fromTime = "05:50";  // format hh:mm
    var toTime = "06:21";
    var ft = fromTime.split(":");
    var tt = toTime.split(":");
    var OK = false;
    if (tt[0] > ft[0]) {OK=true}
    if ((tt[0] == ft[0]) && (tt[1] > ft[1])) {OK = true}
    if (OK) {
    alert ("The second time is after the first time");
    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 ("Difference is " + hr + ":" + mn);
    }
    else {alert ("The second time is NOT after the first time")}
    
    </script>
    "The louder he talked of his honour, the faster we counted our spoons." - Samuel Johnson
    Last edited by Philip M; 01-19-2012 at 07:58 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    narinas (01-19-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    Double-posted How to validate time where to-time must be greater than from-time.

    Please look at forum rule #1.2


    Code:
    <script type = "text/javascript">
    
    var fromTime = "05:50";  // format hh:mm
    var toTime = "06:21";
    var ft = fromTime.split(":");
    var tt = toTime.split(":");
    var OK = false;
    if (tt[0] > ft[0]) {OK=true}
    if ((tt[0] == ft[0]) && (tt[1] > ft[1])) {OK = true}
    if (OK) {
    alert ("The second time is after the first time");
    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 ("Difference is " + hr + ":" + mn);
    }
    else {alert ("The second time is NOT after the first time")}
    
    </script>
    "The louder he talked of his honour, the faster we counted our spoons." - Samuel Johnson
    how to use the code in the above format as from_time and to_time is ind==1 and ind==2

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by narinas View Post
    how to use the code in the above format as from_time and to_time is ind==1 and ind==2
    Obviously if you are going to compare two times, both of the times must be available. Your validation script checks the format of one or the other time, according to ind == 1 or ind == 2.

    You need to wrap the script I gave you within a function, and call it when the format of both the times has been validated. Or re-write the validation script. I sense that you don't know much Javascript and may perhaps find that difficult. This is an example of why it is often difficult to adapt pre-written code to something it was not originally designed to do.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts
    if i give from time as "6:00" to time as "08:00" then its showing wrong.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by narinas View Post
    if i give from time as "6:00" to time as "08:00" then its showing wrong.
    The time format is supposed to be hh:mm with leading zeroes (06:00). That is the proper way to show 24-hour clock time, and is the pattern required by your validation - var patt=/^([0][0-9]
    If you want to allow times with no leading zeroes (6:00), then

    Code:
    <script type = "text/javascript">
    
    var fromTime = "06:00";  // format hh:mm
    var toTime = "08:00";
    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) {
    alert ("The second time is after the first time");
    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 ("Difference is " + hr + ":" + mn);
    }
    else {alert ("The second time is NOT after the first time")}
    
    </script>
    Last edited by Philip M; 01-19-2012 at 12:46 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    narinas (01-19-2012)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •