Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Limit the input in a numeric field

_Marechal_Micha
Level 2
Level 2
Hello,



I would like limit the user input in a numeric field to 10 character for example, but I want verify this limit dynamically.



It's for that I try to work with the event change. I succeed to do that :



var value= xfa.event.newText;

var nb= value.length;

if(nb>10)

{

xfa.host.messageBox("Limit : 10 figures");

}



I succeed to get the value but I fail to change the value to delete the last input character.



If you have any idea to help me 🙂

However, I don't want use the comb property.



Thanks



Regards,

Michaël
8 Replies
pguerett
Level 10
Level 10
That method will test each char as it is entered (not very efficient). It might make more sense to run the script on the exit event then test the value that was entered.



//get the field value into a string variable

var strTest = this.rawValue

//test the length of the string

if (strTest.length >= 10){

//tell the user they are only allowed 10 chars

xfa.host.messageBox("Only 10 digits allowed...");

//truncate the string to 10 chars

this.rawValue = strTest.substr(0,9);

}
_Marechal_Micha
Level 2
Level 2
I thought of this method but I prefer to check every user input, I find it easier for the user.



But if there is no more solution, I will have to take this solution.



Thanks
pguerett
Level 10
Level 10
Then simply substr the value they add beyond 10 in your test (like I did in my example).
_Marechal_Micha
Level 2
Level 2
Hi,



I'm sorry but I have always the same problem.

I tested the code given by Paul, but I don't understand why but it doesn't work. If I try to show the value of strTest.length, it's 0.



Why the length of the value is 0? and why the code doesn't work with me? 😞



Thanks

Michaël
pguerett
Level 10
Level 10
send your form to livecycle8@gmail.com and I will take a look. Please include a description of the issue
JohnBrinkman
Level 2
Level 2
If you want to swallow all keystrokes after you've reached 10 characters, then clear the xfa.event.change property:



if (xfa.event.newText.length > 10)

xfa.event.change = "";



John Brinkman

http://blogs.adobe.com/formfeed
Not applicable
You could use:



if(xfa.event.newText > 9999999999 || xfa.event.newText < -9999999999) { xfa.event.change = "";}



This will allow the field to fill to 10 characters and then disallow further entries. Is this what you're looking for?



Dave
_Marechal_Micha
Level 2
Level 2
Thank you very much, John and David, for your solutions.

It works very well !!!



Have a good day.



Regards,

Michael