Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

Insert an additional character to a text field value

Avatar

Former Community Member

How do I make a function to insert a character into the middel of a text?

I want something like this:

The user types “999999AAAA” and leaves the field.

A messagebox asks “did you mean 999999-AAAA?”, the users choose “yes” and a hyphen is inserted in the text.

I have made a regex to look for the “999999AAAA” and a messagebox but how can I change the field value to have a dash in the middle?

var myRegex = /\d{6}[a-zA-Z]{4}/;

if (this.rawValue != null || this.rawValue != ''){

    if (this.rawValue.match(myRegex)){

        var nChoice = xfa.host.messageBox("Did you mean 999999-AAAA?", "Messagebox title",2,2);

        if (nChoice === 4)

        {

            Some function to add the hyphen

        }

        if (nChoice === 3)

        {

        xfa.host.setFocus(nextField);

        }

    }

}

Regards,

Kirstine

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

I found a useful script here which made it posible for me to make the function I wanted.

var myRegex = /\d{6}[a-zA-Z]{4}/;


if (this.rawValue != null || this.rawValue != '')

    if (this.rawValue.match(myRegex)){

        var nChoice = xfa.host.messageBox("Did you mean 999999-AAAA?", "Messagebox title",2,2);
  if (nChoice === 4)
  {
   function isEmpty(str) {
   if(str == null || str.length == 0) {
    return true;
   }
   return false;
   }
   try
   {
   var vEID = this.rawValue;

   if (!isEmpty(vEID)){
   this.rawValue = vEID.substr(0,6)+"-"+vEID.substr(6).toUpperCase();
   }
   }
   catch(e){
   app.alert(e)
   }
   xfa.host.setFocus(nextField);
   }
  if (nChoice === 3)
  {
 
        xfa.host.setFocus(nextField);
  }
    }

}

View solution in original post

4 Replies

Avatar

Level 10

Hi Kirstine,

Could use just specify a display pattern in Designer?

Have a look at this example for changing the display patern at runtime: http://assure.ly/fRtTyB. You could incorporate this in your if/else statement.

Hope that helps,

Niall

Avatar

Former Community Member

I could, if I was shure the input always would match that pattern but I can’t be. Most inputs will match the pattern 999999-AAAA but the field should be open for all other input patterns as well.

Avatar

Level 10

Okay,

Try this Display Pattern:

text{999999-AAAA}|text{XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX}

This should work automatically without script.

Niall

Avatar

Correct answer by
Former Community Member

I found a useful script here which made it posible for me to make the function I wanted.

var myRegex = /\d{6}[a-zA-Z]{4}/;


if (this.rawValue != null || this.rawValue != '')

    if (this.rawValue.match(myRegex)){

        var nChoice = xfa.host.messageBox("Did you mean 999999-AAAA?", "Messagebox title",2,2);
  if (nChoice === 4)
  {
   function isEmpty(str) {
   if(str == null || str.length == 0) {
    return true;
   }
   return false;
   }
   try
   {
   var vEID = this.rawValue;

   if (!isEmpty(vEID)){
   this.rawValue = vEID.substr(0,6)+"-"+vEID.substr(6).toUpperCase();
   }
   }
   catch(e){
   app.alert(e)
   }
   xfa.host.setFocus(nextField);
   }
  if (nChoice === 3)
  {
 
        xfa.host.setFocus(nextField);
  }
    }

}

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----