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

Validate numeric values only.

New Here ,
Dec 09, 2016 Dec 09, 2016

I have a form that requires only the last 4 digits of a social. I am using a custom validation script to check for 4 values in length:

event.rc = true;
if (event.value.length == 4){
event.target.textColor = color.black; }
    else {
    app.alert("The entered value needs to be 4 characters long.");            

event.target.textColor = color.red; }

The customer has requested to allow a dash character "-" before the numbers and still validate that 4 numbers have been added. This is in case the user wants to add a "-" before the numbers. I removed the format for numbers and set the limit to 5 characters, but now it validates the "-" before the numbers as the length and if 3 numbers are entered it renders true which is wrong. Example, -123.

Is there a way to only count the numbers entered and nothing else?

TOPICS
Acrobat SDK and JavaScript , Windows
1.1K
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

correct answers 1 Correct answer

Community Expert , Dec 09, 2016 Dec 09, 2016

You can use this code:

event.rc = true;

if (event.value) {

    if (/^-?\d{4}$/.test(event.value)) {

        event.target.textColor = color.black;

    } else {

        app.alert("The entered value needs to be 4 characters long.");          

        event.target.textColor = color.red;

        event.rc = false;

    }

}

Translate
Community Expert ,
Dec 09, 2016 Dec 09, 2016

You can use this code:

event.rc = true;

if (event.value) {

    if (/^-?\d{4}$/.test(event.value)) {

        event.target.textColor = color.black;

    } else {

        app.alert("The entered value needs to be 4 characters long.");          

        event.target.textColor = color.red;

        event.rc = false;

    }

}

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
New Here ,
Dec 09, 2016 Dec 09, 2016

Works perfectly. Thanks a lot!

So is that just a mask applied to the value?

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 ,
Dec 09, 2016 Dec 09, 2016

No, it's not a mask. It's a Regular Expression that's validating that the input value has the correct format. The user needs to add the hyphen themselves, if they want to have it. The script won't add it for them, but it will allow it.

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
New Here ,
Dec 09, 2016 Dec 09, 2016

Wow, that's great. I don't want to take up too much of your time, but could you break down how that expression works? Or is there a good place to find out how it functions?

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 ,
Dec 09, 2016 Dec 09, 2016
LATEST

You can read up about it here: JavaScript RegExp 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