Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Adding dashes as numbers are typed

Avatar

Level 7

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

1 Accepted Solution

Avatar

Correct answer by
Level 7

OK. I got this figured out. I had to adjust what was being backspaced over. (i.e., instead of just backspacing over the previous character, I set it to backspace over a selection.)


//test input


var r = new RegExp("[0-9]");


if (!r.test(xfa.event.newText)) xfa.event.change=""; 




//watch input for deleting charactersace


if (xfa.event.newText.length < xfa.event.prevText.length) {//if the number of characters gets smaller


  if (xfa.event.newText.length%5 == 4 && xfa.event.newText.length !=0) {


  //also want to be sure that the character removed was a dash/hyphen


  xfa.event.selEnd = xfa.event.prevText.length;


  xfa.event.selStart = xfa.event.newText.length -1; 


//add in a dash if needed


else if (xfa.event.newText.length%5 == 4) xfa.event.change = xfa.event.change + "-";



View solution in original post

1 Reply

Avatar

Correct answer by
Level 7

OK. I got this figured out. I had to adjust what was being backspaced over. (i.e., instead of just backspacing over the previous character, I set it to backspace over a selection.)


//test input


var r = new RegExp("[0-9]");


if (!r.test(xfa.event.newText)) xfa.event.change=""; 




//watch input for deleting charactersace


if (xfa.event.newText.length < xfa.event.prevText.length) {//if the number of characters gets smaller


  if (xfa.event.newText.length%5 == 4 && xfa.event.newText.length !=0) {


  //also want to be sure that the character removed was a dash/hyphen


  xfa.event.selEnd = xfa.event.prevText.length;


  xfa.event.selStart = xfa.event.newText.length -1; 


//add in a dash if needed


else if (xfa.event.newText.length%5 == 4) xfa.event.change = xfa.event.change + "-";