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 8 of 8
  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Time auto formatting and validation

    Please help me to this in Time validation auto formatting means if he enters 9p then it has to auto format to 9:30 PM and some more rules here

    1. Allows user to enter time of day as ‘A’ or ‘AM’, ‘P’ or ‘PM’
    2. If user enters ‘N’ it will be converted to ‘PM’
    3. Minutes must have 2 digits
    4. 2 digit hour is not required
    5. ‘:’ is not required as a separator between hours and minutes
    6. Will only accept 12 hour times. E.g. will accept 1P, will not accept 1300
    7. Upon field exit TAP will automatically display time in correct. Examples of possible time inputs –
    8. User enters 3P and TAP will change to 3:00 PM.
    9. User enters 120P and TAP will change to 1:20PM. If user intended time to be 12P they can see the time in the corrected format and can modify.


    Please help me to achieve all this !!!

    Please help me as soon as possible

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    I don't understand if he enters 9p then it has to auto format to 9:30 PM - I assume you mean if he enters 930p. However, this should do the trick.

    Code:
    Enter time of day + A or P <input type = "text" name = "tim" id = tim" onblur ="auto(this)"><br>
    <input type = "text" name = "adjtim" id = "adjtim">
    
    <script type = "text/javascript">
    function auto(which) {
    var fintim;
    var ampm = "AM";
    var t = which.value;
    t = t.toUpperCase();
    t = t.replace(/N/,"P");
    t = t.replace(/^0/,"");  // no leading zero
    t = t.replace(/[:\.\-]/g,"");  //strip colon hyphen dot
    if (!/^\d{1,4}[AP]$/i.test(t)) {
    alert ("You must enter a time followed by A or P for AM or PM");
    which.value = "";
    which.focus();
    return false;
    }
    
    var d = t.indexOf("A");
    if (d == -1) {
    var d = t.indexOf("P");
    ampm = "PM";
    }
    
    var hm = t.substring(0,d);
    if (hm.length <= 2) {
    fintim = hm + ":00"
    }
    
    if (hm.length == 3) {
    fintim = hm.substr(0,1) + ":" + hm.substr(1,2);
    }
    
    if (hm .length == 4) {
    if (hm.substr(0,2) <=12) {
    fintim = hm.substr(0,2) + ":" + hm.substr(2,2);
    }
    else {
    alert ("You must enter a time 1 - 12 followed by A or P for AM or PM");  // time over 12 hours entered
    which.value = "";
    which.focus();
    return false;
    }
    }
    
    fintim = fintim + ampm;
    document.getElementById("adjtim").value = fintim;
    
    }
    
    </script>

    Quizmaster: What three-letter word means "at this moment"?
    Contestant: Then.
    Last edited by Philip M; 04-15-2009 at 08:50 AM.

  • Users who have thanked Philip M for this post:

    nandhakumar (04-15-2009)

  • #3
    New Coder
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks Philip.

    It works fine. Please ignore my previous Personal message, before testing i have mailed you.

    Thanks a lot one more time
    Last edited by nandhakumar; 04-15-2009 at 02:36 PM.

  • #4
    New Coder
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Can you please help me to update the same validation for 10:30AM to 10:30 AM or 10AM to 10:00 AM

    Thanks in advance
    Last edited by nandhakumar; 04-21-2009 at 10:01 AM.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by nandhakumar View Post
    Can you please help me to update the same validation for 10:30AM to 10:30 AM or 10AM to 10:00 AM
    What does that mean? When the user enters 10a or 1000a or 10:00a it returns 10:00AM, which is what you requested.

    1. Allows user to enter time of day as ‘A’ or ‘AM’, ‘P’ or ‘PM’
    2. If user enters ‘N’ it will be converted to ‘PM’


    If you now want that the user can enter an optional M after A or P, change this line:-

    if (!/^\d{1,4}[AP]M?$/i.test(t)) {



    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's voluntary unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.
    Last edited by Philip M; 04-21-2009 at 11:27 AM.

  • #6
    New Coder
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    It all comes as a new requirement (changes to the logic implemented), so i have asked for that and also i said thanks since it works with the conditions given previous now it have been enhanced.

  • #7
    New Coder
    Join Date
    Apr 2009
    Posts
    26
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Please help me to update the regular expression, it is giving error on entry of 10:30 AM which is right time.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    See post #5.

    If you now want that the user can enter an optional M after A or P, change this line:-

    if (!/^\d{1,4}[AP]M?$/i.test(t)) {

    it is giving error on entry of 10:30 AM
    No space is allowed between 30 and AM
    You may care to adjust this line so:-
    t = t.replace(/[:\.\-\s]/g,""); //strip colon hyphen dot and space
    or better still:-
    t = t.replace(/[^0-9AMP]/gi,"") ; // strip all non-relevant characters
    Last edited by Philip M; 06-03-2009 at 08:48 AM.


  •  

    Posting Permissions

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