Expand my Community achievements bar.

Testing input data in a text field against approved characters.

Avatar

Level 2

Hello all,

I want to test input in a text field for allowing approved letters in a text field. The field works for the approved letters.

But I want to be able to allow the entry of an asterisk (*) as well.

What changes should I make to the code so that it doesn't reject the inputting of an asterisk?

Here is the code on the change event in JavaScript:

if (xfa.event.newText.match (/[^ABCDEIRna\+\-]/))  {

     xfa.event.change ="";

     xfa.host.beep("3");

}

On the exit event I have this code in FormCalc:

if($ eq "A+" or $ eq "A+" or $ eq "A-" or $ eq "B+" or $ eq "B" or $ eq "B-" or $ eq "C+" or $ eq "C" or $ eq "C-" or $ eq "D+" or $ eq "D" or $ eq "D-" or $ eq "E+" or $ eq "E" or $ eq "E-" or $ eq "R+" or $ eq "I" or $ eq "na" or  $ eq null)

then $ = $

else

     xfa.host.messageBox("Please insert a mark of A+, A, A-, B+, B, B-, C+, C, C-, D+, D, D-, E+, E, E-, R, I or use na.","Data Error - Incorrect Mark", 1, 0)

$ = null

endif

Any help would be appreciated.

9 Replies

Avatar

Level 8

Try this:

if (xfa.event.change!="*" && !xfa.event.newText.match(/^[ABCDEIR](\+|-)?$|^(na?)$/))  {

     xfa.event.change ="";

     xfa.host.beep("3");

}

If you just use this in your change event, you won't even need any script in your exit event.

I'm not sure where the asterisk would go so I just put in a condition to allow it anywhere.

Also, not sure if you wanted the test to be case sensitive. If you want it to be case-insensitive, just put an 'i' after the slash (/) at the end of the regular expression.

If you wanted too, you could put this.rawValue=this.rawValue.toUpperCase() on the exit event of the field. (JavaScript)

Kyle

Avatar

Level 2

Thanks so far, Kyle.

I've been able to incorporate the use of the asterisk on the change event. Awesome!

However, I would like to keep the exit event script intact, so that the users get a warning if they try inserting letters other than the ones indicated, including if they mistakenly use the "8" instead of the *.

How do i set up the exit event that includes the warning about the use of the limited letters, including the asterisk, and at the same time make sure the values return an uppercase value? Where do i include the suggested Java script that pushed the entry to uppercase?

Here's the script so far, but needs to include the uppercase script and the inclusion of the asterisk:

On the exit event

if($ eq "A+" or $ eq "A+" or $ eq "A-" or $ eq "B+" or $ eq "B" or $ eq "B-" or $ eq "C+" or $ eq "C" or $ eq "C-" or $ eq "D+" or $ eq "D" or $ eq "D-" or $ eq "E+" or $ eq "E" or $ eq "E-" or $ eq "na" or  $ eq null)

then $ = $

else

     xfa.host.messageBox("Please insert a mark of A+, A, A-, B+, B, B-, C+, C, C-, D+, D, D-, E+, E, E-, or use na.","Data Error - Incorrect Mark", 1, 0)

$ = null

endif

Thanks for the help so far. Much appreciated!

Avatar

Level 8

Well, with the script I gave you for the change event, there would be no need to validate on the exit event because they cannot even put a wrong character if they tried. So, if they did press 8 instead of asterisk, the 8 wouldn't even show up!

Also the upper case script would go in the exit event (make sure the language is JavaScript).

Let me know if that makes sense to you.

Kyle

Avatar

Level 2

I can successfully use your script so far: I put the script for the change event as follows (just like your script):

if (xfa.event.change!="*" && !xfa.event.newText.match(/^[ABCDE](\+|-)?$|^(na?)$/))  {      xfa.event.change ="";      xfa.host.beep("3"); }

The script on the exit event reads as follows

this.rawValue=this.rawValue.toUpperCase()

So far, the alpha letters default to upper case - this is good - but the field accepts all the letters, not just the ABCDE as well as all the variations of A+ A- etc. It also accepts the asterisk, but also accepts the numeral 8. What do I need to do to limit the alpha letters to A B C D E and na, as well as the asterisk? (The field is limited to 2 characters)

A warning message also pops up on the exit event to warn users that the entry has failed. I can do without the warning popup, as long as I can get the other scripts to function.

Avatar

Level 8

In my form with that exact script, I can put nothing but A, B, C, D, E, asterisk, na, and minus(-)/plus ( +) sign but only after A, B, C, D, E. I can't get any possible configuration of characters where I can get a number.

If you want you can post your form here and I can take a look.

Kyle

Avatar

Level 2

Hello Kyle, I've posted the file. Thanks (please keep the file private.)

Avatar

Level 2

Hi Kyle,

I should have mentioned that I've been trying the scripts on the form I sent you in the term1 achieveOT1 field. All of the Achievement row fields are to return only the ABCDE + -  * na 

I'm still trying your script. If I enter "na" lower case, the field automatically updates it to upper case. If I enter the ABCDE etc as well as the asterisk in lower case, I have to hit the caps key in order for the alpha characters to switch to uppercase. I wonder if I've missed something. It doesn't matter whether I include or exclude the exit.event script. 

The other row fields are to return only digits and are working as they should.

Avatar

Level 8

So I put my code in both change event and exit event into your term1/achieve0T1 field and it works perfectly for me. I made the match case-insensitive since the exit event will capitalize it.

I normally post the form back on my website but since it is private let me know your email and I can send it to you.

Kyle

Avatar

Level 2

Hello Kyle,

I worked with the script in the revised form you so kindly looked at. The problem with entering an asterisk as one of allowable characters is solved. Thank you.

However, there continues to be the limitation I was afraid would continue.

A user can enter the characters (a, b, c, d, e and "na") and they will change to uppercase. They can also enter the "-" sign as the second allowable character (for example: A-, B-, C-, D-, D-). However, in order to allow the entry of a+ (which should automatically uppercase to A+), the "+" sign doesn't work. The user has to use the shift key to enable the "+" sign. This is also true for when the user enters the "*" character. This is true for the symbols on the character side of the keyboard.

Is there a way to set up the script so that the "+" and "*" are included in the automatic script for uppercasing all characters in the field?

My second test: I can use the number pad to include the "*" and the "-" and the "+" without any problem!! Rather than using the keyboard for the symbols, I think this is workable.

So for now, I would call this a success. Thank you, Kyle!

Message was edited by: NellievD on April 4 at 4:34 p.m. after testing the form file.