Let be more tolerant with predefined phone number format
The long US phone number format should be '(999) 999-9999' but the parenthesis can be omitted or the space replaced with a dash, ...
This commit is contained in:
parent
3001264801
commit
3dd8752283
@ -465,22 +465,22 @@ class AForm {
|
|||||||
// specific to the format because the user could enter 1234567 when the
|
// specific to the format because the user could enter 1234567 when the
|
||||||
// format is 999-9999.
|
// format is 999-9999.
|
||||||
const simplifiedFormatStr = cMask.replaceAll(/[^9AOX]/g, "");
|
const simplifiedFormatStr = cMask.replaceAll(/[^9AOX]/g, "");
|
||||||
this.#AFSpecial_KeystrokeEx_helper(simplifiedFormatStr, false);
|
this.#AFSpecial_KeystrokeEx_helper(simplifiedFormatStr, null, false);
|
||||||
if (event.rc) {
|
if (event.rc) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
event.rc = true;
|
event.rc = true;
|
||||||
this.#AFSpecial_KeystrokeEx_helper(cMask, true);
|
this.#AFSpecial_KeystrokeEx_helper(cMask, null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
#AFSpecial_KeystrokeEx_helper(cMask, warn) {
|
#AFSpecial_KeystrokeEx_helper(cMask, value, warn) {
|
||||||
if (!cMask) {
|
if (!cMask) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const event = globalThis.event;
|
const event = globalThis.event;
|
||||||
const value = this.AFMergeChange(event);
|
value ||= this.AFMergeChange(event);
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -563,7 +563,8 @@ class AForm {
|
|||||||
const event = globalThis.event;
|
const event = globalThis.event;
|
||||||
psf = this.AFMakeNumber(psf);
|
psf = this.AFMakeNumber(psf);
|
||||||
|
|
||||||
let formatStr;
|
let value = this.AFMergeChange(event);
|
||||||
|
let formatStr, secondFormatStr;
|
||||||
switch (psf) {
|
switch (psf) {
|
||||||
case 0:
|
case 0:
|
||||||
formatStr = "99999";
|
formatStr = "99999";
|
||||||
@ -572,11 +573,8 @@ class AForm {
|
|||||||
formatStr = "99999-9999";
|
formatStr = "99999-9999";
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
const value = this.AFMergeChange(event);
|
formatStr = "999-9999";
|
||||||
formatStr =
|
secondFormatStr = "(999) 999-9999";
|
||||||
value.startsWith("(") || (value.length > 7 && /^\p{N}+$/.test(value))
|
|
||||||
? "(999) 999-9999"
|
|
||||||
: "999-9999";
|
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
formatStr = "999-99-9999";
|
formatStr = "999-99-9999";
|
||||||
@ -584,8 +582,36 @@ class AForm {
|
|||||||
default:
|
default:
|
||||||
throw new Error("Invalid psf in AFSpecial_Keystroke");
|
throw new Error("Invalid psf in AFSpecial_Keystroke");
|
||||||
}
|
}
|
||||||
|
const formats = secondFormatStr
|
||||||
|
? [formatStr, secondFormatStr]
|
||||||
|
: [formatStr];
|
||||||
|
for (const format of formats) {
|
||||||
|
this.#AFSpecial_KeystrokeEx_helper(format, value, false);
|
||||||
|
if (event.rc) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.rc = true;
|
||||||
|
}
|
||||||
|
|
||||||
this.AFSpecial_KeystrokeEx(formatStr);
|
const re = /([-()]|\s)+/g;
|
||||||
|
value = value.replaceAll(re, "");
|
||||||
|
for (const format of formats) {
|
||||||
|
this.#AFSpecial_KeystrokeEx_helper(
|
||||||
|
format.replaceAll(re, ""),
|
||||||
|
value,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
if (event.rc) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.rc = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.AFSpecial_KeystrokeEx(
|
||||||
|
((secondFormatStr && value.match(/\d/g)) || []).length > 7
|
||||||
|
? secondFormatStr
|
||||||
|
: formatStr
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
AFTime_FormatEx(cFormat) {
|
AFTime_FormatEx(cFormat) {
|
||||||
|
|||||||
@ -1685,6 +1685,67 @@ describe("Scripting", function () {
|
|||||||
send_queue.delete(refId);
|
send_queue.delete(refId);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should validate a US phone number with digits and dashes (long) on a keystroke event", async () => {
|
||||||
|
const refId = getId();
|
||||||
|
const data = {
|
||||||
|
objects: {
|
||||||
|
field: [
|
||||||
|
{
|
||||||
|
id: refId,
|
||||||
|
value: "",
|
||||||
|
actions: {
|
||||||
|
Keystroke: [`AFSpecial_Keystroke(2);`],
|
||||||
|
},
|
||||||
|
type: "text",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
appInfo: { language: "en-US", platform: "Linux x86_64" },
|
||||||
|
calculationOrder: [],
|
||||||
|
dispatchEventName: "_dispatchMe",
|
||||||
|
};
|
||||||
|
sandbox.createSandbox(data);
|
||||||
|
|
||||||
|
let value = "";
|
||||||
|
const changes = "123-456-7890";
|
||||||
|
let i = 0;
|
||||||
|
|
||||||
|
for (; i < changes.length; i++) {
|
||||||
|
const change = changes.charAt(i);
|
||||||
|
await sandbox.dispatchEventInSandbox({
|
||||||
|
id: refId,
|
||||||
|
value,
|
||||||
|
change,
|
||||||
|
name: "Keystroke",
|
||||||
|
willCommit: false,
|
||||||
|
selStart: i,
|
||||||
|
selEnd: i,
|
||||||
|
});
|
||||||
|
expect(send_queue.has(refId)).toEqual(true);
|
||||||
|
send_queue.delete(refId);
|
||||||
|
value += change;
|
||||||
|
}
|
||||||
|
|
||||||
|
await sandbox.dispatchEventInSandbox({
|
||||||
|
id: refId,
|
||||||
|
value,
|
||||||
|
change: "A",
|
||||||
|
name: "Keystroke",
|
||||||
|
willCommit: false,
|
||||||
|
selStart: i,
|
||||||
|
selEnd: i,
|
||||||
|
});
|
||||||
|
expect(send_queue.has(refId)).toEqual(true);
|
||||||
|
expect(send_queue.get(refId)).toEqual({
|
||||||
|
id: refId,
|
||||||
|
siblings: null,
|
||||||
|
value,
|
||||||
|
selRange: [i, i],
|
||||||
|
});
|
||||||
|
|
||||||
|
send_queue.delete(refId);
|
||||||
|
});
|
||||||
|
|
||||||
it("should validate a US phone number (short) on a keystroke event", async () => {
|
it("should validate a US phone number (short) on a keystroke event", async () => {
|
||||||
const refId = getId();
|
const refId = getId();
|
||||||
const data = {
|
const data = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user