mirror of https://github.com/Budibase/budibase.git
2 changed files with 27 additions and 0 deletions
@ -0,0 +1,15 @@ |
|||||
|
import { writable, derived } from 'svelte/store' |
||||
|
|
||||
|
export function createValidationStore(initialValue, ...validators) { |
||||
|
|
||||
|
const value = writable(initialValue || '') |
||||
|
const error = derived(value, $v => validate($v, validators)) |
||||
|
|
||||
|
return [value, error] |
||||
|
} |
||||
|
|
||||
|
function validate(value, validators) { |
||||
|
const failing = validators.find(v => v(value) !== true) |
||||
|
|
||||
|
return failing && failing(value) |
||||
|
} |
||||
@ -0,0 +1,12 @@ |
|||||
|
function emailValidator (value) { |
||||
|
return (value && !!value.match(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)) || 'Please enter a valid email' |
||||
|
} |
||||
|
|
||||
|
function requiredValidator (value) { |
||||
|
return (value !== undefined && value !== null && value !== '') || 'This field is required' |
||||
|
} |
||||
|
|
||||
|
export { |
||||
|
emailValidator, |
||||
|
requiredValidator |
||||
|
} |
||||
Loading…
Reference in new issue