Web Analytics Made Easy -
StatCounter Time auto formatting and validation - CodingForum

Announcement

Collapse
No announcement yet.

Time auto formatting and validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    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; Apr 15, 2009, 03:50 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.

    Comment


    • #3
      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; Apr 15, 2009, 09:36 AM.

      Comment


      • #4
        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; Apr 21, 2009, 05:01 AM.

        Comment


        • #5
          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; Apr 21, 2009, 06:27 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.

          Comment


          • #6
            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.

            Comment


            • #7
              Please help me to update the regular expression, it is giving error on entry of 10:30 AM which is right time.

              Comment


              • #8
                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; Jun 3, 2009, 03:48 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.

                Comment

                Working...
                X