Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Regex and JavaScript

Community Expert ,
Mar 22, 2025 Mar 22, 2025

I have a regex problem.
In my form (attached), after selecting the country in the drop-down list, the user has to enter a code which must correspond to the pattern of his country.
The pattern is passed as a variable from the drop-down menu value.

 

In this example, I have a problem with Argentina, Australia and Austria: if the user enters too few digits, the entry is invalidated. As expected, this is OK.


But if the user enters too many digits, the entry is not invalidated, e.g. for Australia he has to enter 8 or 9 digits, but if he enters more it is not invalidated and this is not OK.

 

I've tried with several different regexes, for example for Australia I use :
\\d\{8,9\}
or
\\d\\d\\d\\d\\d\\d\\d\\d(\\d)?
but the issue is still the same.

 

Is this a bug or have I missed something?

Thank you for your help.

 

Here are the scripts I use to fill in the drop-down list (from the console):

// test 1
var c = this.getField("P2.pays1-fiscal-liste");
c.setItems([["Albanie", "\[A-Z\]\\d\\d\\d\\d\\d\\d\\d\\d\[A-Z\]"],["Andorre", "(E|F)\\d\\d\\d\\d\\d\\d\[A-Z\]"],["Argentine", "2(0|3|4|5|6|7)\\d{9,9}"],["Australie", "\\d\{8,9}"],["Autriche", "\\d{9,9}"]]);

// test 2
var c = this.getField("P2.pays1-fiscal-liste");
c.setItems([["Albanie", "\[A-Z\]\\d\\d\\d\\d\\d\\d\\d\\d\[A-Z\]"],["Andorre", "(E|F)\\d\\d\\d\\d\\d\\d\[A-Z\]"],["Argentine", "2(0|3|4|5|6|7)\\d\\d\\d\\d\\d\\d\\d\\d\\d"],["Australie", "\\d\\d\\d\\d\\d\\d\\d\\d(\\d)?"],["Autriche", "\\d\\d\\d\\d\\d\\d\\d\\d\\d"]]);

 

This is the Validation script used in the text field:

var string = this.getField("P2.pays1-fiscal-liste").valueAsString;
console.println("string: " + string);
var regex = new RegExp(string);
console.println("regex: " + regex);
if (event.value) {
	console.println("event.value: " + event.value);
	var valid = regex.test(event.value);
	if (!valid) app.alert("Invalid value.");
	event.rc = valid;
}

 

Here are the patterns to follow, where

X = any letter

9 = any digit

Capture_2503221929.pngexpand image


Acrobate du PDF, InDesigner et Photoshopographe
TOPICS
JavaScript , PDF , PDF forms
428
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 22, 2025 Mar 22, 2025

You should use the String Start and End markers in your RegExp, eg. instead of /\d{8,9}/, use /^\d{8,9}$/ .

The former means "the string must contain 8 or 9 digits in a row". The latter means "the string must be 8 or 9 digits in a row, and nothing else".

 

Edit: added the closing and opening forward-slashes and made the RegExps bold, so it's more clear where they start and end.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 22, 2025 Mar 22, 2025

You should use the String Start and End markers in your RegExp, eg. instead of /\d{8,9}/, use /^\d{8,9}$/ .

The former means "the string must contain 8 or 9 digits in a row". The latter means "the string must be 8 or 9 digits in a row, and nothing else".

 

Edit: added the closing and opening forward-slashes and made the RegExps bold, so it's more clear where they start and end.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 23, 2025 Mar 23, 2025

Thank you, you saved my form !

But it took me a while to figure out that I actually need to use
^\d{8,9}$
because the new RegExp() function automatically adds a backslash at the beginning and end of the string/pattern.


Acrobate du PDF, InDesigner et Photoshopographe
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 23, 2025 Mar 23, 2025
LATEST

Yes, if you use the RegExp builder then you don't need the slashes, as it takes the content of the expression as an input parameter in the form of a simple string, but you do if you define it as a literal object.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines