formdatavalid

FormData validation

Usage no npm install needed!

<script type="module">
  import formdatavalid from 'https://cdn.skypack.dev/formdatavalid';
</script>

README

FormValid

Install

npm install formdatavalid

Use

import FormDataValid, {Validation} from 'formdatavalid'

const formData = new FormData(element)

const loginValid = FormDataValid(formData, {
    login: Validation.required,
    password: Validation.required
})

if(loginValid.has()){
    ...
} else {
    console.log(loginValid.get(), loginValid.toString())
}

Rules

accepted

The field under validation must be "yes", "on", 1, or true. This is useful for validating "Terms of Service" acceptance or similar fields.

after(field_name)

The field under validation must be a value after a given date.

after_to_equal(field_name)

The field under validation must be a value after or equal to the given date.

array

The field under validation must be a array.

before(field_date)

The field under validation must be a value preceding the given date.

before_to_equal(field_name)

The field under validation must be a value preceding or equal to the given date.

between(min, max)

The field under validation must have a size between the given min and max. Strings, numerics and arrays are evaluated in the same fashion as the size rule.

boolean

The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".

confirmed

The field under validation must have a matching field of {field}_confirmation. For example, if the field under validation is password, a matching password_confirmation field must be present in the input.

date

The field under validation must be a valid, non-relative date according to the strtotime.

different(field_name)

The field under validation must have a different value than field.

email

The field under validation must be formatted as an email address.

in(value1, value2, ...)

The field under validation must be included in the given list of values.

max(max)

The field under validation must be less than or equal to a maximum value. Strings, numerics and arrays are evaluated in the same fashion as the size rule.

min(min)

The field under validation must have a minimum value. Strings, numerics and arrays are evaluated in the same fashion as the size rule.

not_in(value1, value2, ...)

The field under validation must not be included in the given list of values.

number

The field under validation must be numeric.

regex(pattern)

The field under validation must match the given regular expression.

required

The field under validation must be present in the input data and not empty.

required_with(field_name1, field_name2, ...)

The field under validation must be present and not empty only if any of the other specified fields are present and not empty.

required_with_all(field_name1, field_name2, ...)

The field under validation must be present and not empty only if all of the other specified fields are present and not empty.

required_without(field_name1, field_name2, ...)

The field under validation must be present and not empty only when any of the other specified fields are empty or not present.

required_without_all(field_name1, field_name2, ...)

The field under validation must be present and not empty only when all of the other specified fields are empty or not present.

phone(pattern)

The field under validation must be phone. Default format: '+7 (000) 000-00-00'

same(field_name)

The given field must match the field under validation.

size(size)

The field under validation must have a size matching the given value. For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value (the attribute must also have the numeric or integer rule). For an array, size corresponds to the count of the array.

string

The field under validation must be a string. If you would like to allow the field to also be null, you should assign the nullable rule to the field.