Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. Text replace

Text replace

Scheduled Pinned Locked Moved JavaScript
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rakeshs312
    wrote on last edited by
    #1

    I have a text box andit will accept only time in the format 00:00(maximum value can be 12:59) . If user enter any of the value greater than this that part only needs to become 00. i have the code like this,but it will change the whole value to 00:00 . In the greater than conditions, what i have to write .

    function ValidateTimeControl(s, e) {
    var isValid = true;
    if (e.value != null) {
    if (e.value.substring(0, 1).trim() == "" || e.value.substring(1, 2).trim() == "" || e.value.substring(3, 4).trim() == "" || e.value.substring(3, 4).trim() == "") {
    isValid = false;
    }
    else {
    var hours = parseInt(e.value.substring(0, 2), 10);
    var minutes = parseInt(e.value.substring(3, 5), 10);

            if (hours > 12) {alert(hours)
                isValid = false;
                
            }
    
            if (minutes > 59) {
                isValid = false;
            }
        }
    }
    else {
        isValid = false;
    }
    
    if (!isValid) {
        e.value = '00:00';
        
    }
    

    }

    Thanks Rks

    L 1 Reply Last reply
    0
    • R rakeshs312

      I have a text box andit will accept only time in the format 00:00(maximum value can be 12:59) . If user enter any of the value greater than this that part only needs to become 00. i have the code like this,but it will change the whole value to 00:00 . In the greater than conditions, what i have to write .

      function ValidateTimeControl(s, e) {
      var isValid = true;
      if (e.value != null) {
      if (e.value.substring(0, 1).trim() == "" || e.value.substring(1, 2).trim() == "" || e.value.substring(3, 4).trim() == "" || e.value.substring(3, 4).trim() == "") {
      isValid = false;
      }
      else {
      var hours = parseInt(e.value.substring(0, 2), 10);
      var minutes = parseInt(e.value.substring(3, 5), 10);

              if (hours > 12) {alert(hours)
                  isValid = false;
                  
              }
      
              if (minutes > 59) {
                  isValid = false;
              }
          }
      }
      else {
          isValid = false;
      }
      
      if (!isValid) {
          e.value = '00:00';
          
      }
      

      }

      Thanks Rks

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to rewrite the text value with the hours or minutes set to zero. Something like:

      else {
      var hours = parseInt(e.value.substring(0, 2), 10);
      var minutes = parseInt(e.value.substring(3, 5), 10);

      if (hours > 12) {
          alert(hours)
          hours = 0;
      }
      if (minutes > 59) {
          minutes = 0;
      
      }
      // I'm not sure about this but you can probably figure out the exact statement
      e.value = hours.toString() + ":" + minutes.toString();
      

      }

      One of these days I'm going to think of a really clever signature.

      R 1 Reply Last reply
      0
      • L Lost User

        You need to rewrite the text value with the hours or minutes set to zero. Something like:

        else {
        var hours = parseInt(e.value.substring(0, 2), 10);
        var minutes = parseInt(e.value.substring(3, 5), 10);

        if (hours > 12) {
            alert(hours)
            hours = 0;
        }
        if (minutes > 59) {
            minutes = 0;
        
        }
        // I'm not sure about this but you can probably figure out the exact statement
        e.value = hours.toString() + ":" + minutes.toString();
        

        }

        One of these days I'm going to think of a really clever signature.

        R Offline
        R Offline
        rakeshs312
        wrote on last edited by
        #3

        Thanks for the reply . i Found a solution like this

        function ValidateTimeControl(s, e) {
        var isValidHour = true;
        var isValidMin = true;
        var hours, minutes;
        if (e.value != null) {
        if (e.value.substring(0, 1).trim() == "" || e.value.substring(1, 2).trim() == "" ) {
        isValidHour = false;
        }
        if (e.value.substring(3, 4).trim() == "" || e.value.substring(3, 4).trim() == "") {
        isValidMin = false;
        }
        else {
        hours = parseInt(e.value.substring(0, 2), 10);
        minutes = parseInt(e.value.substring(3, 5), 10);

                if (hours > 12) {alert(hours)
                    isValidHour = false;
                    
                }
        
                if (minutes > 59) {
                    isValidMin = false;
                    
                }
            }
        }
        else {
            isValidHour = false;
            isValidMin = false;
        }
        
        if (isValidHour == false && isValidMin == false) {
            e.value = '00:00';
        } else if (isValidHour == false) {
            e.value = e.value.replace(hours, '00');
        } else if (isValidMin == false) {
            e.value = e.value.replace(minutes, '00');
        }
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups