Expand my Community achievements bar.

SOLVED

how do i limit a textfield to specific alpha characters of one entry but in a textfield that needs to have a limit of two spaces in order to allow for a two-letter combination?

Avatar

Level 2

I want to force the user to limit the entries in a textfield to use only four single alphabet entries (E, C, W, or A), in addition to a two-letter alphabet entry "na". I have a textfield with a limit of two characters. Right now, it is possible for the user to incorrectly enter two of the same alpha entries. How do i restrict the user to entering just one entry, while at the same time allowing the user to enter the "na" entry?

1 Accepted Solution

Avatar

Correct answer by
Level 3

Oh okay, I thought that was simply a consequence of you only allowing one alphabetic entry, never understood you wanted it as a feature to have the "n" autocomplete to "na".

Did you miswrite this last comment? As above you have stated that "I had hoped to limit the 'na' entry to lower case". Is this simply for the entry? You want the output to always be uppercase?

If this is the case you simply just have to remove the if statement on the exit event making this.rawValue.toUpperCase() trigger everytime. And seeing you want to limit the entry for "na" to a single "n", you can remove the "na" or statement in the "input verification".

On change:

if (!xfa.event.newText.match(/^(e|c|w|a|n)$/i))

{

    xfa.event.change = "";

}

On exit:

if (this.rawValue == "n"){

this.rawValue.replace(/n/g,"na")

}

this.rawValue=this.rawValue.toUpperCase()

Are you sure this wouldn't "trick" the user? wanting to entry "na" and only being allowed to entry "n" ?

Also in your code you seem to have double "replace(/n/g,"na")" statements? One outside the if statement and one inside? From my point of view you should only need one. As at the moment it will evaluate to "naa" in the case that the user enters "na" (I have tested this in a dummy form).

Maybe this is because you are thinking that you want to limit the user to only be able to enter the single input "n".

If you feel your question is answered please feel free to mark an answer as correct so that the thread will be tagged correctly aswell (I guess you have all the tools you need to find the exact behaviour you are looking for).

Best regards, Mattias.

View solution in original post

5 Replies

Avatar

Level 3

Forgive me for not fully understanding the premise on how your form currently works. I understand what you want to achieve with it, but when you say it is possible to "enter two of the same alpha entries" is this limited to the four letters you want to allow? E.g. have you limited the form already to only accept the E,C,W or A ?

Anyhow what I would suggest is simply a validation of the users entry on the "exit" event of the textfield. So upon exiting, do some kind of if statements like for example:

if(form1.page.TextField1.rawValue == "na" || form1.page.TextField1.rawValue == "A" || form1.page.TextField1.rawValue == "C || form1.page.TextField1.rawValue == "E" || form1.page.TextField1.rawValue == "W"){

}

else{

     form1.page.TextField1.rawValue = ""

     xfa.host.messageBox("Please enter a valid choice (E,C,W,A or na)."

}

The messageBox is just an example of the verification. That can be done in a few other ways depending on how you like it aswell.

If you have any questions, feel free to ask.

If this solves your issue, please mark it as the correct answer.

Best regards, Mattias

Avatar

Level 2

On the exit event i have the following script:

this.rawValue=this.rawValue.replace(/n/g,"na")

this.rawValue=this.rawValue.toUpperCase()

On the change event I have entererd the following script:

// Test input in real time. Only allow approved grades and na

if (xfa.event.newText.match(/[^ewcan]/)) {

    xfa.event.change = "";

    xfa.host.messageBox("Only E,W,C,A, na are valid entries. NOTE, N outputs NA.","A reminder",1);

}

I have limited the entry to 2 characters in order to accommodate the "na" but also need to limit the single letter entries E,W,C,A to only entry in the same text field. As well, I had hoped to limit the "na" entry to lower case.

Do you have a suggestion?

I will also be trying your suggestion while I hope for some additional input....

Thank you, Mattias.

When I try the script, it allows more than one entry of the letter E,W,C,or A without throwing an error message. The entry needs to be limited to just one letter, with the exception of the "na" entry.

****  I've tried another approach:

On the change event:

if (!xfa.event.newText.match(/^(e|e\d{1,3}|c|c\d{1,4}|w|w\d{1,4}|a|a\d{1,4}|\d{1,5})$/i))

{

    xfa.event.change = "";

}

On the exit event:

this.rawValue=this.rawValue.replace(/n/g,"na")

this.rawValue=this.rawValue.toUpperCase()

So I am getting all the correct information, but the "na" is also being changed to uppercase.

What do I have to change in the script to keep the "na" lowercase??

Thanks for the help!

Avatar

Level 3

It honestly seems you got better scripting knowledge than me as these match and replace functions are  a bit new to me.

When trying your script, I get the behaviour you describe with the grades, but I fail to get a "na" in there in any way?

And I understand your code as - In the change event, check if the "newText" buffer contains "e" "c" "w" or "a" case insensitive else "clear" the buffer. And this seems to be working good, it only accepts one entry of the actual grades and then on the exit you want to replace /n/ globally with na? And make it upper case?

When having entered your code in an example script of mine, I can't seem to get na in there at all? And honestly I can't see the way "n" is supposed to be allowed in the change event? To get it to work, I had to add in the n in the change verification?

I did some tweaking and this is what I ended up with

On the change event:

if (!xfa.event.newText.match(/^(e|c|w|a|n|na)$/i)){

    xfa.event.change = "";

}

On the exit event:

if (this.rawValue == "n"){

    this.rawValue = ""

}

if (this.rawValue != "na"){

    this.rawValue=this.rawValue.toUpperCase()

}

from my testing it seems to be working exactly as you want it.

Back to your example script it is a bit unclear what you want to achieve with the "e\d{1,3}" match patterns? as that allows you to have a letter e followed by 3 numbers? Is this what you intended, or am I missing something?

Best regards, Mattias.

Avatar

Level 2

I had hoped to limit the entry for "na" to one entry of "n" which would then automatically become an "na" and also become uppercase.

I worked with your suggestions and came up with this.I tested the result and I believe it works properly:

On change*

if (!xfa.event.newText.match(/^(e|c|w|a|n|na)$/i))

{

    xfa.event.change = "";

}

On exit:

this.rawValue=this.rawValue.replace(/n/g,"na")

if (this.rawValue == "n"){

    this.rawValue.replace(/n/g,"na")

}

if (this.rawValue != "na"){

    this.rawValue=this.rawValue.toUpperCase()

}

Thank you.

Avatar

Correct answer by
Level 3

Oh okay, I thought that was simply a consequence of you only allowing one alphabetic entry, never understood you wanted it as a feature to have the "n" autocomplete to "na".

Did you miswrite this last comment? As above you have stated that "I had hoped to limit the 'na' entry to lower case". Is this simply for the entry? You want the output to always be uppercase?

If this is the case you simply just have to remove the if statement on the exit event making this.rawValue.toUpperCase() trigger everytime. And seeing you want to limit the entry for "na" to a single "n", you can remove the "na" or statement in the "input verification".

On change:

if (!xfa.event.newText.match(/^(e|c|w|a|n)$/i))

{

    xfa.event.change = "";

}

On exit:

if (this.rawValue == "n"){

this.rawValue.replace(/n/g,"na")

}

this.rawValue=this.rawValue.toUpperCase()

Are you sure this wouldn't "trick" the user? wanting to entry "na" and only being allowed to entry "n" ?

Also in your code you seem to have double "replace(/n/g,"na")" statements? One outside the if statement and one inside? From my point of view you should only need one. As at the moment it will evaluate to "naa" in the case that the user enters "na" (I have tested this in a dummy form).

Maybe this is because you are thinking that you want to limit the user to only be able to enter the single input "n".

If you feel your question is answered please feel free to mark an answer as correct so that the thread will be tagged correctly aswell (I guess you have all the tools you need to find the exact behaviour you are looking for).

Best regards, Mattias.