Copy link to clipboard
Copied
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?
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;
}
}
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
Works perfectly. Thanks a lot!
So is that just a mask applied to the value?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
You can read up about it here: JavaScript RegExp Object
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more