Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit ae18b9a

Browse files
committed
URL regex validation
1 parent 0730769 commit ae18b9a

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

dev/App.vue

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
email,
5353
pattern,
5454
maxLength,
55+
url,
5556
/* } from '../dist/as-dynamic-forms.common'; */
5657
} from '../src/main';
5758
@@ -109,6 +110,13 @@ const data = () => ({
109110
),
110111
],
111112
}),
113+
new FormField({
114+
type: 'url',
115+
label: 'Website',
116+
name: 'website',
117+
customClass: 'col-12',
118+
validations: [new FormValidation(url, `Format of Url is incorrect`)],
119+
}),
112120
new FormField({
113121
type: 'select',
114122
label: 'Category',

src/components/dynamic-input/DynamicInput.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
formControl.type === 'text' ||
1313
formControl.type === 'email' ||
1414
formControl.type === 'password' ||
15+
formControl.type === 'url' ||
1516
formControl.type === 'number'
1617
"
1718
:formControl="formControl"

src/core/utils/validators.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const isEmptyInputValue = value =>
22
value == null || value === '' || value.length === 0;
33

44
const EMAIL_REGEXP = /^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(\.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[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])?)*$/;
5+
const URL_REGEXP = /^((?:(https?):\/\/)?((?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[0-9][0-9]|[0-9]))|(?:(?:(?:\w+\.){1,2}[\w]{2,3})))(?::(\d+))?((?:\/[\w]+)*)(?:\/|(\/[\w]+\.[\w]{3,4})|(\?(?:([\w]+=[\w]+)&)*([\w]+=[\w]+))?|\?(?:(wsdl|wadl))))$/;
56

67
export const required = control =>
78
isEmptyInputValue(control.value) ? { required: true } : null;
@@ -36,6 +37,13 @@ export const email = control => {
3637
return EMAIL_REGEXP.test(control.value) ? null : { email: true };
3738
};
3839

40+
export const url = control => {
41+
if (isEmptyInputValue(control.value)) {
42+
return null; // don't validate empty values to allow optional controls
43+
}
44+
return URL_REGEXP.test(control.value) ? null : { email: true };
45+
};
46+
3947
export const minLength = minLength => control => {
4048
if (isEmptyInputValue(control.value)) {
4149
return null; // don't validate empty values to allow optional controls
@@ -90,4 +98,5 @@ export default {
9098
minLength,
9199
maxLength,
92100
pattern,
101+
url,
93102
};

0 commit comments

Comments
 (0)