跳到主要内容

Add state from zip code

Adds the "State" label and the text form prefilled with "CA" if the zip code of California is entered. It can be extended by using API to prefill "State" when entering the zip code.

(function()
{
let doc = Api.GetDocument();
let allForms = doc.GetAllForms();
//California Zip Codes regex
let zipRegex = /^9[0-5][0-9]{3}$/;

allForms.forEach(form => {
if(form.GetFormKey() == "ZipCode") {
let input = form.GetText();
//If the zip code is Californian, add the "State" label with the text form indicating "CA"
if(zipRegex.test(input)) {
let stateForm = allForms.find(item => item.GetFormKey() === "State");
if(stateForm == null) {
let stateTextForm = Api.CreateTextForm({"key": "State", "tip": "State", "required": false, "placeholder": "State", "comb": true, "maxCharacters": 2, "cellWidth": 3, "multiLine": false, "autoFit": false});
stateTextForm.SetText("CA");
let paragraph = Api.CreateParagraph();
paragraph.AddTabStop();
paragraph.AddText("State: ");
paragraph.AddElement(stateTextForm);
doc.InsertContent([paragraph]);
}
else {
stateForm.SetText("CA");
}
}
}
});
})();

Methods used: GetDocument, GetAllForms, GetFormKey, GetText, CreateTextForm, SetText,, CreateParagraph, AddTabStop, AddText, AddElement, InsertContent

Result

Add State From ZipCodeAdd State From ZipCode