tl;dr: I'm trying to make a script that will add dashes as numbers are being typed in a field and remove them and the previous number as the user backspaces over the dashes.
I have a form that I'm working on which includes a field for a credit card number. I want to add a dash (hyphen) as the user types the credit card number. I can get the adding part to work, but I can't get the field to backspace over the dash AND the previous number. I think the problem lies with my (lack of) understanding how xfa.event.newText and xfa.event.change work.
Example: The user has entered "12345". The dash is automatically added by script to give us "1234-5". The user wants to change 4 to 6. When the user reaches the "-" I want the backspace to take the dash as well as the previous number--the 4. That way, when the user has just "123" and types 6, the dash will be added automatically (the script already handles this part).
Here's what I've got so far. (The text field is a comb of 19 characters.)
if (xfa.event.newText.length < xfa.event.prevText.length) {
if (xfa.event.newText.lastIndexOf("-") == (xfa.event.newText.length - 1)) {
this.rawValue = xfa.event.newText.slice(0,(xfa.event.newText.lastIndexOf("-")-1);
}
}
else if (xfa.event.newText.length == 4 || xfa.event.newText.length == 9 || xfa.event.newText.length == 14) xfa.event.change = xfa.event.change + "-";
Message was edited by: Jason Rana Corrected a typo in the code