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
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);
}
}
}