Skip to content

Commit

Permalink
Merge pull request #12719 from calixteman/emailvalidate
Browse files Browse the repository at this point in the history
JS -- add function eMailValidate used to validate an email address
  • Loading branch information
timvandermeij authored Dec 10, 2020
2 parents e53ae01 + f94269c commit 954ac3d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/scripting_api/aform.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class AForm {
"m/d/yy HH:MM",
];
this._timeFormats = ["HH:MM", "h:MM tt", "HH:MM:ss", "h:MM:ss tt"];

// The e-mail address regex below originates from:
// https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
this._emailRegex = new RegExp(
"^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+" +
"@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?" +
"(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$"
);
}

_parseDate(cFormat, cDate) {
Expand Down Expand Up @@ -577,6 +585,10 @@ class AForm {
this.AFDate_KeystrokeEx(this._timeFormats[pdf]);
}
}

eMailValidate(str) {
return this._emailRegex.test(str);
}
}

export { AForm };
2 changes: 1 addition & 1 deletion src/scripting_api/initialization.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function initSandbox({ data, extra, out }) {
out.zoomtype = ZoomType;

for (const name of Object.getOwnPropertyNames(AForm.prototype)) {
if (name.startsWith("AF")) {
if (name !== "constructor" && !name.startsWith("_")) {
out[name] = aform[name].bind(aform);
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/unit/scripting_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1207,5 +1207,21 @@ describe("Scripting", function () {
}
});
});

describe("eMailValidate", function () {
it("should validate an e-mail address", function (done) {
Promise.all([
myeval(`eMailValidate(123)`).then(value => {
expect(value).toEqual(false);
}),
myeval(`eMailValidate("foo@bar.com")`).then(value => {
expect(value).toEqual(true);
}),
myeval(`eMailValidate("foo bar")`).then(value => {
expect(value).toEqual(false);
}),
]).then(() => done());
});
});
});
});

0 comments on commit 954ac3d

Please sign in to comment.