A simple, Laravelian, way to validate JavaScript forms.
npm i @mikkjal/validate.js
const form = {
email: 'test@example.com',
password: 'Haken 123',
password_confirmation: '',
};
const rules = {
email: 'required|email',
password: 'required|min:8|confirmed',
};
const $v = validate(form, rules);
{
"valid": false,
"email": {
"required": true,
"email": true
},
"password": {
"required": true,
"min": true,
"confirmed": false
}
}
const form = {
name: 'mikkjal',
job: {
title: 'Full-stack Developer',
email: 'mikkjal@example.com',
},
};
let rules = {
name: 'required',
job: {
title: 'required|ends_with:Developer',
email: 'required|email',
},
email: 'required|email',
};
// or
rules = {
name: 'required',
'job.title': 'required|ends_with:Developer',
'job.email': 'required|email',
email: 'required|email',
};
const $v = validate(form, rules);
{
"valid": false,
"name": {
"required": true
},
"job": {
"title": {
"required": true,
"ends_with": true
},
"email": {
"required": true,
"email": true
}
},
"email": {
"required": false,
"email": false
}
}
const form = {
name: 'mikkjal',
personal_email: '',
job: {
title: 'Full-stack Developer',
email: 'mikkjal@example.com',
},
};
const $v = validate(form, {
'*email': 'email',
'job.*': 'required',
});
{
"valid": false,
"personal_email": {
"email": false
},
"job": {
"email": {
"required": true,
"email": true
},
"title": {
"required": true
}
}
}
const form = {
email: 'test@example.com',
password: 'Test123',
password_confirmation: '',
work: {
email: '',
},
};
const rules = {
email: 'required',
password: 'required|min:8|confirmed',
'*email': 'email',
};
const messages = {
'email.required': 'Email address is required',
'*email.email': 'Not a valid email address',
'password.min': 'The password needs to be at least 8 characters',
'password.confirmed': 'The passwords do not match',
};
const $v = validate(form, rules, messages);
{
"valid": false,
"email": {
"required": true,
"email": true
},
"password": {
"required": true,
"min": false,
"messages": ["The password needs to be at least 8 characters", "The passwords do not match"],
"confirmed": false
}
}
const rules = {
dob: 'after:2000-01-01',
};
const rules = {
text: 'ascii',
};
const rules = {
dob: 'before:2000-01-01',
};
const rules = {
age: 'between:18,122',
};
const rules = {
password: 'confirmed',
};
const rules = {
dob: 'date',
};
const rules = {
password: 'different:username',
};
const rules = {
email: 'email',
};
const rules = {
text: 'ends:world!',
};
const rules = {
color: 'hex',
};
const rules = {
name: 'in:hello,world',
};
const rules = {
ip: 'ip',
};
const rules = {
mac_address: 'mac_address',
};
const rules = {
age: 'max:122',
password: 'max:16',
};
const rules = {
age: 'min:18',
password: 'min:8',
};
const rules = {
name: 'not_in:mikkjal,signar',
};
const rules = {
not_a_number: 'not_regex:/^[0-9]$/',
};
const rules = {
age: 'numeric',
};
const rules = {
number: 'regex:/^[0-9]{6}$/',
};
const rules = {
name: 'required',
};
const rules = {
password: 'required_with:name,email',
};
const rules = {
password: 'required_with_all:name,email',
};
const rules = {
password: 'required_without:name,email',
};
const rules = {
password: 'required_without_all:name,email',
};
const rules = {
password: 'same:password_confirmation',
};
const rules = {
text: 'starts_with:Hello',
};
const rules = {
url: 'url',
};
const rules = {
uuid: 'uuid',
};