Expand my Community achievements bar.

SOLVED

Keeping focus after validation error and alert

Avatar

Level 3

I have some code that works about like I want with one small glitch. The alert works when the character count in incorrect, and it keeps focus on the field. The problem is that after the correct number of characters are entered in the field, it still keeps focus on the field until you tab out of it.

I need it to work like it does, with the exception that when the correct number of characters is entered, it needs to go to the next field automatically.

If the alert is removed, it works correctly, except, of course, I do not get the alert. Here is my code.


var r = new RegExp();
  r.compile("^([0-9]{8})$","i");

var result = r.test(this.rawValue);

if (result == true)

    true;

else
    app.alert("SAP numbers are 8 digits. Please re-enter the SAP number.", 0);
    xfa.host.setFocus(this);

Thanks

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

You need to add braces to the else. Without braces it only executes the first statement so setFocus always executes. As a habit I use braces on all if-else statements.

if (result == true) {

    true;

}

else {

    app.alert("SAP numbers are 8 digits. Please re-enter the SAP number.", 0);

    xfa.host.setFocus(this);

}

Steve

View solution in original post

2 Replies

Avatar

Correct answer by
Former Community Member

You need to add braces to the else. Without braces it only executes the first statement so setFocus always executes. As a habit I use braces on all if-else statements.

if (result == true) {

    true;

}

else {

    app.alert("SAP numbers are 8 digits. Please re-enter the SAP number.", 0);

    xfa.host.setFocus(this);

}

Steve

Avatar

Level 3

That did it. Sometimes it is just something that simple. I thought I had tried it with the brackets, but I guess I didn't. Thanks

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] ----