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