Form Validation Rules
Below are 52 form validation rules and code examples to help you implement them into your form.
List of Validation Rules
Validation Examples
accepted
The field under validation must be true.
<input type="checkbox" name="terms" rules="accepted"> I agree to the terms and conditions.
<input type="checkbox" name="terms" data-kw-rules="accepted"> I agree to the terms and conditions.
after_or_equal:date
The field under validation must be a value after or equal to the given date.
alpha
The field under validation must be entirely alphabetic characters (only letters).
<input name="username" rules="alpha">
<input name="username" data-kw-rules="alpha">
alpha_dash
The field under validation may have alpha, numeric, dash and underscore characters.
<input name="username" rules="alpha_dash">
<input name="username" data-kw-rules="alpha_dash">
alpha_num
The field under validation must be entirely alpha-numeric characters.
<input name="username" rules="alpha_num">
<input name="username" data-kw-rules="alpha_num">
array
The field under validation must be an array.
bail
Stop running validation rules after the first validation failure.
before:date
The field under validation must be a value preceding the given date. Like the "after" rule, the name of another field under validation may be supplied as the value of date
.
between:min,max
The field under validation must have a size between the given min and max.
- For string data, value is the number of characters.
- For numeric data, value is a given integer value.
- For an array, value is the count of the array.
- For files, value is the file size in kilobytes.
<input type="number" name="age" rules="numeric|between:21,30">
<input type="number" name="age" data-kw-rules="numeric|between:21,30">
File upload example
<input type="file" name="myfile" rules="file|between:400,1000">
<input type="file" name="myfile" data-kw-rules="file|between:400,1000">
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 with a name of foo_confirmation
. For example, if the field under validation is email
, a matching email_confirmation
field must be present in the input.
<input type="email" name="email" rules="email|confirmed"> <input type="email" name="email_confirmation" rules="email">
<input type="email" name="email" data-kw-rules="email|confirmed"> <input type="email" name="email_confirmation" data-kw-rules="email">
date
The field under validation must be a valid, non-relative date.
different:field
The field under validation must have a different value than field. In this example "lunch" must be different than "dinner".
<select name="lunch" rules="required"> <option value="sandwhich">sandwhich</option> <option value="salad">salad</option> <option value="chicken and rice">chicken and rice</option> </select>
<select name="dinner" rules="required|different:lunch"> <option value="sandwhich">sandwhich</option> <option value="salad">salad</option> <option value="chicken and rice">chicken and rice</option> </select>
<select name="lunch" data-kw-rules="required"> <option value="sandwhich">sandwhich</option> <option value="salad">salad</option> <option value="chicken and rice">chicken and rice</option> </select>
<select name="dinner" data-kw-rules="required|different:lunch"> <option value="sandwhich">sandwhich</option> <option value="salad">salad</option> <option value="chicken and rice">chicken and rice</option> </select>
digits:value
The field under validation must be numeric and must have an exact length of value.
<input type="number" name="year" rules="required|digits:4">
<input type="number" name="year" data-kw-rules="required|digits:4">
digits_between:min,max
The field under validation must have a length between the given min and max.
<input type="number" name="favorite_numbrt" rules="required|digits_between:1,2" >
<input type="number" name="favorite_number" data-kw-rules="required|digits_between:1,2" >
The field under validation must be formatted as an e-mail address.
<input type="email" name="email_address" rules="email" >
<input type="email" name="email_address" data-kw-rules="email" >
file
The field under validation must be a successfully uploaded file.
This rule validates against these common file types: jpg, jpeg, gif, png, bmp, tif, psd, pdf, doc, docx, xls, xlsx, txt, mp3, mp4, aac, wav, au, wmv, avi, mpg, mpeg, zip, gz, rar, z, tgz, tar, sitx.
<input type="file" name="my_file" rules="file" >
<input type="file" name="my_file" data-kw-rules="file" >
gt:field
The field under validation must be greater than the given field. The two fields must be of the same type.
- For string data, value is the number of characters.
- For numeric data, value is a given integer value.
- For an array, value is the count of the array.
- For files, value is the file size in kilobytes.
<input type="number" name="min_price" rules="required"> <input type="number" name="max_price" rules="required|gt:min_price">
<input type="number" name="min_price" data-kw-rules="required"> <input type="number" name="max_price" data-kw-rules="required|gt:min_price">
gte:field
The field under validation must be greater than or equal to the given field. The two fields must be of the same type.
- For string data, value is the number of characters.
- For numeric data, value is a given integer value.
- For an array, value is the count of the array.
- For files, value is the file size in kilobytes.
image
The file under validation must be an image (jpeg, png, bmp, gif, or svg)
<input type="file" name="my_file" rules="image" >
<input type="file" name="my_file" data-kw-rules="image" >
in_array:anotherfield.*
The field under validation must exist in anotherfield's values.
integer
The field under validation must be an integer.
ip
The field under validation must be an IP address. This rule accepts ipv4 and ipv6.
ipv4
The field under validation must be an IPv4 address.
ipv6
The field under validation must be an IPv6 address.
json
The field under validation must be a valid JSON string.
lt:field
The field under validation must be less than the given field. The two fields must be of the same type.
- For string data, value is the number of characters.
- For numeric data, value is a given integer value.
- For an array, value is the count of the array.
- For files, value is the file size in kilobytes.
lte:field
The field under validation must be less than or equal to the given field. The two fields must be of the same type.
- For string data, value is the number of characters.
- For numeric data, value is a given integer value.
- For an array, value is the count of the array.
- For files, value is the file size in kilobytes.
link
The field under validation must contain a valid URL link. There are two modes of validation:
-
link
- Basic URL validation that checks for simple domain patterns -
link:strict
- Stricter URL validation that requires URLs to start withhttp://
,https://
, orwww.
max:value
The field under validation must be less than or equal to a maximum value.
- For string data, value is the number of characters.
- For numeric data, value is a given integer value.
- For an array, value is the count of the array.
- For files, value is the file size in kilobytes.
<input type="text" name="username" rules="max:5" >
<input type="text" name="username" data-kw-rules="max:5" >
Numeric example
<input type="number" name="age" rules="numeric|max:18" >
<input type="number" name="age" data-kw-rules="numeric|max:18" >
File upload example
<input type="file" name="myfile" rules="file|max:400" >
<input type="file" name="myfile" data-kw-rules="file|max:400" >
mimetypes:text/plain
The file under validation must match one of the given MIME types.
A full listing of MIME types and their corresponding extensions can be seen here: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
<input type="file" name="my_file" rules="mimetypes:text/plain" >
<input type="file" name="my_file" data-kw-rules="mimetypes:text/plaine" >
mimes:foo,bar
The file under validation must have a specified extension. More than one extension can be specified separated by commas.
A full listing of MIME types and their corresponding extensions can be seen here: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
<input type="file" name="my_file" rules="mimes:jpg,png" >
<input type="file" name="my_file" data-kw-rules="mimes:jpg,png" >
min:value
The field under validation must have a minimum value.
- For string data, value is the number of characters.
- For numeric data, value is a given integer value.
- For an array, value is the count of the array.
- For files, value is the file size in kilobytes.
<input type="text" name="username" rules="min:5">
<input type="text" name="username" data-kw-rules="min:5">
Numeric example
<input type="number" name="age" rules="numeric|min:18" >
<input type="number" name="age" data-kw-rules="numeric|min:18" >
File upload example
<input type="file" name="myfile" rules="file|min:400">
<input type="file" name="myfile" data-kw-rules="file|min:400">
no_link
The field under validation must not contain any URL links. There are two modes of validation:
-
no_link
- Basic URL pattern detection to block simple domain patterns -
no_link:strict
- Stricter URL pattern detection that blocks URLs starting withhttp://
,https://
, orwww.
not_regex:pattern
The field under validation must not match the given regular expression.
Internally, this rule uses the PHP preg_match function. The pattern specified should obey the same formatting required by preg_match and thus also include valid delimiters. For example: rules="not_regex:/^.+$/i"
nullable
The field under validation may be null. This is particularly useful when validating primitive such as strings and integers that can contain null values.
numeric
The field under validation must be numeric.
phone
The field under validation must be a valid phone number. You can specify the type of phone number validation:
-
phone:us
- Validates US phone numbers -
phone:international
- Validates international phone numbers -
phone
orphone:all
- Validates both US and international formats
Valid US Phone Number Formats:
- (123) 456-7890
- (123) 4567890
- 123-456-7890
- 1234567890
Valid International Phone Number Formats:
- +1 (123) 456-7890
- +44 20 7123 4567
- +86 10 1234 5678
- +33 1 23 45 67 89
regex:pattern
The field under validation must match the given regular expression.
Internally, this rule uses the PHP preg_match function. The pattern specified should obey the same formatting required by preg_match and thus also include valid delimiters. For example: rules="regex:/^.+@.+$/i"
.
required
The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:
- The value is null.
- The value is an empty string.
- The value is an empty array or empty Countable object.
- The value is an uploaded file with no path.
<input name="name" rules="required">
<input name="name" data-kw-rules="required">
required_if:anotherfield,value,...
The field under validation must be present and not empty if the anotherfield field is equal to any value.
required_unless:anotherfield,value,...
The field under validation must be present and not empty unless the anotherfield field is equal to any value.
required_with:foo,bar,...
The field under validation must be present and not empty only if any of the other specified fields are present.
required_with_all:foo,bar,...
The field under validation must be present and not empty only if all of the other specified fields are present.
required_without:foo,bar,...
The field under validation must be present and not empty only when any of the other specified fields are not present.
required_without_all:foo,bar,...
The field under validation must be present and not empty only when all of the other specified fields are not present.
same:field
The given field must match the field under validation.
size:value
The field under validation must have a size exactly matching the given value.
- For string data, value is the number of characters.
- For numeric data, value is a given integer value.
- For an array, value is the count of the array.
- For files, value is the file size in kilobytes.
<input type="text" name="username" rules="size:5" >
<input type="text" name="username" data-kw-rules="size:5" >
Numeric example
<input type="number" name="age" rules="numeric|size:18" >
<input type="number" name="age" data-kw-rules="numeric|size:18" >
File upload example
<input type="file" name="myfile" rules="file|size:400" >
<input type="file" name="myfile" data-kw-rules="file|size:400" >
starts_with:foo,bar,...
The field under validation must start with one of the given values.
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.
timezone
The field under validation must be a valid timezone identifier according to the timezone_identifiers_list PHP function.
url
The field under validation must be a valid URL.
uuid
The field under validation must be a valid RFC 4122 (version 1, 3, 4, or 5) universally unique identifier (UUID).
Notice anything wrong in our docs? Let us know.