Skip to main content
Participant
December 9, 2016
Answered

Validate numeric values only.

  • December 9, 2016
  • 1 reply
  • 1070 views

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?

This topic has been closed for replies.
Correct answer try67

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;

    }

}

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 9, 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;

    }

}

Participant
December 9, 2016

Works perfectly. Thanks a lot!

So is that just a mask applied to the value?

try67
Community Expert
Community Expert
December 9, 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.