How do I create an if statement alert for a dropdown where the validation is: must be 12 characters and start with 25, 35, or 45?

New Here ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

This is what I have so far however I do not know how to get the 35 and 45 in the statement.

I am using Adobe Pro XI. This is my first time adding scripts to document for validation.

event.rc = true;
if (event.value.length != 12 && event.value != 'Must be 12 digits Select Prefix and enter Account number behind. cheq -  25  ushceq  - 35  plan 24 - 45') {
    app.alert("The entered value must be 12 characters long and start with prefix cheq '25' or uscheq '35' or plan 24 '45'!");
event.rc = false;
}

var n = event.value.indexOf("25");

if (n == -1) { // substring not found
    app.alert("The entered value must be 12 characters long and start with prefix cheq '25' or uscheq '35' or plan 24 '45'");
    event.rc = false;
}
else {
    event.rc = true;

TOPICS
Acrobat SDK and JavaScript

Views

169

Likes

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
Most Valuable Participant ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

You can use the indexOf method that you're already using. It will return the index of the matching position for the term you're searching for. If it appears at the start of the string that will be 0.

Also, I would recommend you allow the event.value.length to be 0, or the user won't be able to clear the field...

Likes

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
New Here ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

If I leave the script with event,value.indexOF("25") and try to enter 451234567898 to make it 12 characters and starts with 45 I am getting the pop up alert and it is correct it starts with 45 and 12 digits. If I enter 12 digits starting with 25 it works. How do I get it to validate the 35 and 45?  Also I need it to validate it is 12 digits, if I change the event,value.length to 0 it allows for more or less than 12 digits.

event.rc = true;
if (event.value.length != 12 && event.value != 'Must be 12 digits Select Prefix and enter Account number behind. cheq -  25  ushceq  - 35  plan 24 - 45') {
    app.alert("The entered value must be 12 characters long and start with prefix cheq '25' or uscheq '35' or plan 24 '45'!");
event.rc = false;
}

var n = event.value.indexOf("25");
if (n == -1) { // substring not found
    app.alert("The entered value must be 12 characters long and start with prefix cheq '25' or uscheq '35' or plan 24 '45'");
    event.rc = false;
}
else {
    event.rc = true;
}

Likes

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
Most Valuable Participant ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

This is not specific for Acrobat - but it does what you want.

var prmt = prompt ("12 digits (start with 25, 35 or 45)", 253456789012, "check Number");

if (prmt.length != 12) {

    alert ("The entered value must be 12 characters long");

    }

if (prmt.match ( /^[234]5/ ) == null ) {

    alert ("""The entered value must be 12 characters long and start with prefix cheq '25' or uscheq '35' or plan 24 '45'""");

    };

Have fun

Likes

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
Most Valuable Participant ,
Nov 24, 2018 Nov 24, 2018

Copy link to clipboard

Copied

LATEST

You can add multiple such conditions in a single if-statement, like this:

if (event.value.indexOf("45")!=0 && event.value.indexOf("25")!=0) ...

Likes

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