I wanna auto add slash in credit card expiry date field input. I wanna add slash after type 2 character and remove slash after delete third digit. Example, 23/ (auto add slash after type number 3) 23/4 (auto remove slash after delete number 4)
addSlashes(elementID) {
let ele = document.getElementById(elementID)
const value = ele.value
let finalVal = null
if (value.length === 2) {
finalVal = `${value}/`
}
document.getElementById(elementID).value = finalVal
},
As soon as the / is added after 2 characters, the total length becomes 3. So, it is not very clear how the operation should happen.
However, if this is for some automatic changes to happen when progressive typing takes place, here is the code to add a / automatically when the user has typed 2 characters and remove it when the next character is added after the /
Edit
OP needs to delete the / when the 3rd digit is removed.
function modifyInput(ele) {
if (ele.value.length === 2)
ele.value = ele.value + '/'
else
if (ele.value.length === 3 && ele.value.charAt(2) === '/')
ele.value = ele.value.replace('/', '');
}
<input type="text" onkeyup="modifyInput(this)">