svelte-validoz
svelte-validoz is Validoz for Svelte for form validation
.
See svelte-yup better library for data validation
Installation
$ npm install svelte-validoz
Sample code
Form Submit
// form.js filelet fields = name: "Email address" type: "email" value: '' // you can pass your custom properties name: "Age" type: "number" value: 12 min: 18 ;
Remove those lines below if you want to revalidate only after submits.
let submited = false;$: ifsubmited result = ;
All messages in one place
Example below to put all messages in one place by AllMessages
component
Form // add it once Submit
Date example
;let field = name: "Date" type: "date" value: '24/05/2020' dateFormat: 'dd/mm/yyyy' startDate: '08/02/2020' endDate: '24/05/2020'; let result = ;console; // { field: 'Date', message: '' }; // true; // true
Types
name | Description |
---|---|
text |
Any characters |
password |
String must contain at least one numberic, one upper case, one lower case characters and the length at least 6 characters |
fullname |
String should contain at least 2 words with 3 characters for each of the words and separated by space. It can contain more than one word. |
username |
Like Instagram username. |
word |
Alphabet characters. |
number |
An integer number |
date |
Example 21/03/2020 string. |
time |
Example 05:12 string. |
Field properties
name | Description |
---|---|
name |
Field name |
value |
Field value |
type |
Field type |
required |
Boolean . default: true |
min and max |
Minumum and Maximum of type number. Each of them can be passed alone. |
minDigits and maxDigits |
Minumum and Maximum digits of type number. Each of them can be passed alone. |
minLength and maxLength |
Minimum and Maximum length of the string types. |
dateFormat |
String values of mm/dd/yyyy , mm-dd-yyyy , dd/mm/yyyy , dd-mm-yyyy , yyyy/mm/dd and yyyy-mm-dd |
equal |
A field value and equal value to be equal. |
notEqual |
A field value and equal value not to be equal. |
Other example
let validoz isValid isValidByName = ; let field = ;let result = ;console; /*Returns: [ { field: 'Full name', message: '' }, { field: 'Email address', message: 'Email address is invalid' }, { field: 'Age', message: 'Age must be between 18 and 60' }, { field: 'Best friend', message: 'Best friend value is wrong' }, { field: 'Password', message: 'Password must contain at least one numberic, one upper case, one lower case characters and the length at least 6 characters' }]*/; // false; // false; // "Full name", true; // true; // false; // false
// form.js filelet field = name: "Full name" type: "text" value: 'Hello world' minLength: 6 name: "Email address" type: "email" value: 'example@.com' name: "Age" type: "number" value: 12 min: 18 max: 60 name: "Best friend" type: "text" value: 'Doe' equal: 'John' // value must be John name: "Password" type: "password" // you can also pass text if you don't want regex pattern to be conditioned value: '123456' minLength: 6 maxLength: 30 ;
Other Examples with source code
Author
Kamyar Lajani
License
MIT