Skip to main content
Known Participant
December 21, 2022
Answered

Regex ^\d{8}$ fails if first digit/digits are zero. Why?

  • December 21, 2022
  • 2 replies
  • 1992 views

Hey thanks for reading.

 

I've tried two reg expressions to find an 8 character numeric entry. Despite working on regexr, neither work in acrobat if the entry starts with 0.

^\d{8}$

or

^[0-9]{8}$
 
So 12345678 works in both.
 
but 01234567 doesn't. Why?
This topic has been closed for replies.
Correct answer try67

My guess is you're using it on the value of a field, and you're accessing it with the value property. Is that correct?
If not, please post your full code, as this reg-exp shouldn't fail on a number that starts with a zero.

2 replies

try67
try67Correct answer
Community Expert
December 21, 2022

My guess is you're using it on the value of a field, and you're accessing it with the value property. Is that correct?
If not, please post your full code, as this reg-exp shouldn't fail on a number that starts with a zero.

dankpoochAuthor
Known Participant
January 2, 2023

Indeed, I was using it on the value. Switching to valueAsString fixed the problem. When the number is converted to a string, it dropped the initial '0' digit, thus the remaining 7digit number failed. 

Thom Parker
Community Expert
December 21, 2022

Are you testing the RegExp on a number?  Regular expressions only work on strings, so any numbers are converted to strings first.  The number 0123 converts to the string 123 or 123.000000 if you're not careful.  

 

Try the RegExp on "01234567". It'll work just fine.  

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often