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

JavaScript Function and Problem with Backspace Button

Scheduled Pinned Locked Moved ASP.NET
javascripthelpquestion
4 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

      First off - this is the wrong forum for this question - you should have posted it in "JavaScript". Secondly, code blocks should be wrapped using the "code block" button above the editor when posting. Having said all that, you code behaves as it does because you are catching the keyup event on the backspace key just like any other. You need your code to be of the form:

      if (event.keyCode == 8) {
      // that block
      } else {
      // the rest
      }

      Also, I suspect you have only been testing/using this in IE - you may need to look at how Firefox sees it...

      A 1 Reply Last reply
      0
      • N NeverHeardOfMe

        First off - this is the wrong forum for this question - you should have posted it in "JavaScript". Secondly, code blocks should be wrapped using the "code block" button above the editor when posting. Having said all that, you code behaves as it does because you are catching the keyup event on the backspace key just like any other. You need your code to be of the form:

        if (event.keyCode == 8) {
        // that block
        } else {
        // the rest
        }

        Also, I suspect you have only been testing/using this in IE - you may need to look at how Firefox sees it...

        A Offline
        A Offline
        AndyASPVB
        wrote on last edited by
        #3

        In reply to your answer, the code will only ever run in IE as it is an internal web application not a website, so testing in Firefox or in Google Chrome is not required. When testing in IE, the javascript is not hitting if the statement and is thus not working as expected.

        N 1 Reply Last reply
        0
        • A AndyASPVB

          In reply to your answer, the code will only ever run in IE as it is an internal web application not a website, so testing in Firefox or in Google Chrome is not required. When testing in IE, the javascript is not hitting if the statement and is thus not working as expected.

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

          Well OK, but you still need to write it along the lines I suggested. Of course, you need also to check for valid input at some stage - eg as is a user could enter 99/99/9999.

          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