Skip to main content
JR Boulay
Community Expert
Community Expert
March 22, 2025
Answered

Regex and JavaScript

  • March 22, 2025
  • 2 replies
  • 633 views

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

Correct answer try67

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.

2 replies

JR Boulay
Community Expert
JR BoulayCommunity ExpertAuthor
Community Expert
March 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
try67
Community Expert
Community Expert
March 23, 2025

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.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
March 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.