Expand my Community achievements bar.

SOLVED

Adobe Form Question about entering text in a field

Avatar

Level 4

I am building a form & it has a first name & last name field. How can I make it so when a user types in a number in the field they get an error telling them they can only enter letters only? Is this possible? If a user types in a number in the name fiels I'd either like nothing to happen or an error pop up. I have the form set so in another field only numbers are allowed & if the user types in a letter in this field nothing happens, it only allows numbers. How would I do this to allow letter only?

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

One way to do this is to use a regex to validate the input. You can put the following code into the exit event of the field. Make sure the language is set to JavaScript:

var pattern = /^[\sa-zA-Z\-]*?$/;

if(!this.rawValue.match(pattern)) {

xfa.host.messageBox("Only letters may be entered into this field.");

this.rawValue = '';

}

The regex ensures that only alpha characters are entered into the field and also allows a hyphen and a space to accommodate some names (for example van der Camp or Parker-bowls). If the regex is not matched, the popup appears. Then lastly the invalid data is cleared.

View solution in original post

2 Replies

Avatar

Correct answer by
Former Community Member

One way to do this is to use a regex to validate the input. You can put the following code into the exit event of the field. Make sure the language is set to JavaScript:

var pattern = /^[\sa-zA-Z\-]*?$/;

if(!this.rawValue.match(pattern)) {

xfa.host.messageBox("Only letters may be entered into this field.");

this.rawValue = '';

}

The regex ensures that only alpha characters are entered into the field and also allows a hyphen and a space to accommodate some names (for example van der Camp or Parker-bowls). If the regex is not matched, the popup appears. Then lastly the invalid data is cleared.

Avatar

Level 2

Hi:

I have the same requirement and this code works fine from the exit event.  However, I need the validation to run for each character entered so as soon as an end user enters a number, they get the message not just on the exit event.

I tried this code which works on the exit event in the validation event, change event and others but it only seems to work on the exit event.

How could I code this to run for each character entered?  Is there another code or event I should be using?

Thank you,

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