Skip to content

Commit dffa001

Browse files
committed
#9529 A Text input field with Numeric mask ignores the maxLength restriction
Fixes #9529
1 parent 52f3962 commit dffa001

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

functionalTests/questions/input_mask.ts

+31
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,37 @@ frameworks.forEach((framework) => {
105105
.expect(Selector("input").value).eql(emptyValue);
106106
});
107107

108+
test("mask and maxlength", async (t) => {
109+
await initSurvey(framework, {
110+
focusFirstQuestionAutomatic: true,
111+
"pages": [
112+
{
113+
"name": "page1",
114+
"elements": [
115+
{
116+
"type": "text",
117+
"name": "question1",
118+
"maskType": "numeric",
119+
"maxLength": 2
120+
}
121+
]
122+
}
123+
]
124+
});
125+
126+
await t
127+
.expect(Selector("input").value).eql("")
128+
129+
.pressKey("1")
130+
.expect(Selector("input").value).eql("1")
131+
132+
.pressKey("2")
133+
.expect(Selector("input").value).eql("12")
134+
135+
.pressKey("3")
136+
.expect(Selector("input").value).eql("12");
137+
});
138+
108139
test("Test mask in western timezone", async (t) => {
109140
if (framework === "vue") return;
110141
const oldTimeZone = await getTimeZone();

packages/survey-core/src/mask/input_element_adapter.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ import { ITextInputParams } from "./mask_utils";
44
export class InputElementAdapter {
55
private prevUnmaskedValue: string = undefined;
66

7+
private setInputValue(value: string) {
8+
if (this.inputElement.maxLength >= 0 && this.inputElement.maxLength < value.length) {
9+
value = value.slice(0, this.inputElement.maxLength);
10+
}
11+
this.inputElement.value = value;
12+
}
713
constructor(private inputMaskInstance: InputMaskBase, private inputElement: HTMLInputElement, value?: any) {
814
let _value: any = value;
915
if (_value === null || _value === undefined) {
1016
_value = "";
1117
}
12-
this.inputElement.value = inputMaskInstance.getMaskedValue(_value);
18+
this.setInputValue(inputMaskInstance.getMaskedValue(_value));
1319
this.prevUnmaskedValue = _value;
1420

1521
inputMaskInstance.onPropertyChanged.add(this.inputMaskInstancePropertyChangedHandler);
@@ -19,7 +25,7 @@ export class InputElementAdapter {
1925
inputMaskInstancePropertyChangedHandler = (sender: any, options: any) => {
2026
if (options.name !== "saveMaskedValue") {
2127
const maskedValue = this.inputMaskInstance.getMaskedValue(this.prevUnmaskedValue);
22-
this.inputElement.value = maskedValue;
28+
this.setInputValue(maskedValue);
2329
}
2430
}
2531

@@ -32,7 +38,7 @@ export class InputElementAdapter {
3238
beforeInputHandler = (event: any) => {
3339
const args = this.createArgs(event);
3440
const result = this.inputMaskInstance.processInput(args);
35-
this.inputElement.value = result.value;
41+
this.setInputValue(result.value);
3642
this.inputElement.setSelectionRange(result.caretPosition, result.caretPosition);
3743
if (!result.cancelPreventDefault) {
3844
event.preventDefault();
@@ -41,7 +47,7 @@ export class InputElementAdapter {
4147

4248
changeHandler = (event: any) => {
4349
const result = this.inputMaskInstance.processInput({ prevValue: "", insertedChars: event.target.value, selectionStart: 0, selectionEnd: 0 });
44-
this.inputElement.value = result.value;
50+
this.setInputValue(result.value);
4551
}
4652

4753
public createArgs(event: any): ITextInputParams {

0 commit comments

Comments
 (0)