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

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

Participant ,
Dec 21, 2022 Dec 21, 2022

Copy link to clipboard

Copied

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?
TOPICS
JavaScript , PDF forms

Views

714

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Dec 21, 2022 Dec 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.

Votes

Translate

Translate
Community Expert ,
Dec 21, 2022 Dec 21, 2022

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

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 ,
Dec 21, 2022 Dec 21, 2022

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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
Participant ,
Jan 02, 2023 Jan 02, 2023

Copy link to clipboard

Copied

LATEST

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. 

Votes

Translate

Translate

Report

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