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. JavaScript Function and Problem with Backspace Button

JavaScript Function and Problem with Backspace Button

Scheduled Pinned Locked Moved JavaScript
javascripthelpquestion
2 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.
  • A Offline
    A Offline
    AndyASPVB
    wrote on last edited by
    #1

    Hi I have a javascript function that is applied to a textbox onkeyup so that when a user puts in a date in the format of dd/mm/yyyy it puts in the forward slashes. It also stops the user exceeding ten characters. However, when the user uses the backspace button, whilst the numbers are deleted, unless you keep down on the button, I can't get the backspace button to move back beyond the slashes. Here is my code, and I have highlighted in bold the bit that is not working:

    function DateInputUpdate(input) {
    if ( input.value.length == 2 || input.value.length == 5 )
    input.value = input.value + dateSep;

       if (input.value.length > dateFormat.length) {
        input.value = input.value.substring(0, dateFormat.length);
    
       **if (event.keyCode == 8) {
         if (input.value.length == 3 || input.value.length == 6) {
            input.value = input.value.substring(input.value.length, input.value.length - 1);**
       }
      }
    }
    

    }

    Can someone please advise me what I need to do or change? I don't particular wish to delete the slashes, more to ignore them or skip over them that's my ideal solution. Thanks

    N 1 Reply Last reply
    0
    • A AndyASPVB

      Hi I have a javascript function that is applied to a textbox onkeyup so that when a user puts in a date in the format of dd/mm/yyyy it puts in the forward slashes. It also stops the user exceeding ten characters. However, when the user uses the backspace button, whilst the numbers are deleted, unless you keep down on the button, I can't get the backspace button to move back beyond the slashes. Here is my code, and I have highlighted in bold the bit that is not working:

      function DateInputUpdate(input) {
      if ( input.value.length == 2 || input.value.length == 5 )
      input.value = input.value + dateSep;

         if (input.value.length > dateFormat.length) {
          input.value = input.value.substring(0, dateFormat.length);
      
         **if (event.keyCode == 8) {
           if (input.value.length == 3 || input.value.length == 6) {
              input.value = input.value.substring(input.value.length, input.value.length - 1);**
         }
        }
      }
      

      }

      Can someone please advise me what I need to do or change? I don't particular wish to delete the slashes, more to ignore them or skip over them that's my ideal solution. Thanks

      N Offline
      N Offline
      NeverHeardOfMe
      wrote on last edited by
      #2

      Now you're just cross-posting, which is a no-no here! I meant that your original post should have been made here, not to repeat it here. And I've already told you how to fix it. Oh well, never mind - here, this works (in IE):

      function DateInputUpdate(input) {
      if (event.keyCode == 8) {
      if (input.value.length == 3 || input.value.length == 6) {
      input.value = input.value.substring(0,input.value.length-1);
      }
      } else {
      if ( input.value.length == 2 || input.value.length == 5 )
      input.value = input.value + '/';
      if (input.value.length > 10) {
      input.value = input.value.substring(0, 10);
      }
      }
      }

      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