Expand my Community achievements bar.

SOLVED

How can I validate numeric values in a text field?

Avatar

Former Community Member

Hi

Once more I have a problem.

I have a field in which the user should enter 10 digits in the pattern 999999-9999. The field must only contain numeric data and must be exactly 10 characters. I have a validation script on the print button, that prevent printing if required fields are empty or not properly filled out.

I have tried two ways to work this out, and I'm not satisfied with any of them, as I can still print despite of validation scripts and warning messages.

1.

I set the field to numeric, limited to 11 characters.

Display pattern num{999999-9999}, edit pattern num{999999-9999} and validation pattern num{999999-9999}

Problems using this solution: When I type 11 digits in the field (ex.12345612345) I get a warning message and 12.345.612.

But when I type 9 digts (ex. 123456-123 or 123456123) I get no warning message and 000000-0000 or 012345-6123

If I type alphabetical characters (ex. 123456-abcd) I get no warning message and 000000-0000.

2.

I set the field to text field, limited to 11 characters.

Display pattern text{999999-9999}, edit pattern text{999999-9999} and validation pattern text{999999-9999}

Now when I type 11 digits in the field (ex.12345612345) I get no warning message and 12345612345.

When I type 9 digts (ex. 123456-123) I get no warning message and 123456-123

When I type 9 digts (ex. 123456123) I get a warning message and 123456123

If I type alphabetical characters (ex. 123456-abcd) I get a warning message and 123456-abcd.

It works slightly better as a text field, but I am not satisfied at all.

Has anyone a solution to this?

Additional I would also like the hyphen to emerge automatically when typing.

K

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

Regular expressions are useful for this type of validation. In the code below tf1 is a text field declared with a maximum length of 12.

// form1.page1.tf1::exit - (JavaScript, client)


if (!(this.isNull)) {

  var tf = this.rawValue;

  // \d to match any character in the range 0 - 9

  var regExp = /\d{6}\-\d{5}/;

  if (!(regExp.test(tf))) {

    xfa.host.messageBox("The field must be in the format 123456-12345.","Validation",1);

  }

}

The expression /\d{6}\-\d{5}/ tests for 6 digits \d{6}, a dash \- and 5 digits \d{5}.

Steve

View solution in original post

3 Replies

Avatar

Correct answer by
Former Community Member

Regular expressions are useful for this type of validation. In the code below tf1 is a text field declared with a maximum length of 12.

// form1.page1.tf1::exit - (JavaScript, client)


if (!(this.isNull)) {

  var tf = this.rawValue;

  // \d to match any character in the range 0 - 9

  var regExp = /\d{6}\-\d{5}/;

  if (!(regExp.test(tf))) {

    xfa.host.messageBox("The field must be in the format 123456-12345.","Validation",1);

  }

}

The expression /\d{6}\-\d{5}/ tests for 6 digits \d{6}, a dash \- and 5 digits \d{5}.

Steve

Avatar

Former Community Member

I just want to summarize my experience.

I found a related discussion here: http://forums.adobe.com/message/2513720#2513720.

Putting the pieces together I came up with this solution.

form1.subform.TextField1::exit - (JavaScript, client)

var

myRegex = /\d{6}\-\d{4}/;

var

singleDigits = [0,1,2,3,4,5,6,7,8,9]; // array for single digit check

 

if

(this.rawValue != null || this.rawValue != ''){ // only if the field has a value...

     if (!this.rawValue.match(myRegex)){ // and it doesnt match the pattern (i.e. 2 digits)

     this.rawValue

= ''; // clear the field

     xfa.host.setFocus(

this); // set focus back to the field

     xfa.host.messageBox('Error: Pattern Conflict. Please use the pattern 999999-9999.');

// enter any message you wish here

     }

}

Kirstine

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