Skip to content

Commit 66ee274

Browse files
committed
minlength maxlength validation for input
1 parent 50e18fb commit 66ee274

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

packages/uui-input/lib/uui-input.element.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,42 @@ export class UUIInputElement extends FormControlMixin(LitElement) {
134134
`,
135135
];
136136

137+
/**
138+
* This is a minimum value of the input.
139+
* @type {number}
140+
* @attr
141+
* @default undefined
142+
*/
143+
@property({ type: Number })
144+
minlength?: number;
145+
146+
/**
147+
* Minlength validation message.
148+
* @type {boolean}
149+
* @attr
150+
* @default
151+
*/
152+
@property({ type: String, attribute: 'minlength-message' })
153+
minlengthMessage = 'This field need more characters';
154+
155+
/**
156+
* This is a maximum value of the input.
157+
* @type {number}
158+
* @attr
159+
* @default undefined
160+
*/
161+
@property({ type: Number })
162+
maxlength?: number;
163+
164+
/**
165+
* Maxlength validation message.
166+
* @type {boolean}
167+
* @attr
168+
* @default
169+
*/
170+
@property({ type: String, attribute: 'maxlength-message' })
171+
maxlengthMessage = 'This field exceeds the allowed amount of characters';
172+
137173
/**
138174
* Disables the input.
139175
* @type {boolean}
@@ -190,6 +226,17 @@ export class UUIInputElement extends FormControlMixin(LitElement) {
190226
this.addEventListener('blur', () => {
191227
this.style.setProperty('--uui-show-focus-outline', '');
192228
});
229+
230+
this.addValidator(
231+
'tooShort',
232+
() => this.minlengthMessage,
233+
() => !!this.minlength && (this._value as string).length < this.minlength
234+
);
235+
this.addValidator(
236+
'tooLong',
237+
() => this.maxlengthMessage,
238+
() => !!this.maxlength && (this._value as string).length > this.maxlength
239+
);
193240
}
194241

195242
/**

0 commit comments

Comments
 (0)