A field validation assigns a series of rules that have been assigned to a particular input and checks, upon the form submission, whether or not the input meets all specified criteria.
field_validation(id, ..., extra_params = NULL)
field_rule(rule, prompt = NULL, value = NULL)
HTML id of the field to be validated
A series of field_rule
s that will be applied to the field
A named list of extra parameters that can be added to the field validation. For example
optional = TRUE
means the field will only be checked if non-empty
The type of rule to be applied. Valid rules are available in Details.
Text to be displayed in the UI if the validation fails. Leave NULL
if want to use default text.
Certain fields require a value to check validation. Check Details if the rule
requires a value.
A structured list of the field_rules
that can be recognised by form_validation
.
If it fails, then the field will be highlighted and the failures will either be specified as a message below the field or a label. Once the failure(s) has been rectified, the highlighting will disappear.
The following rules
are allowed:
empty
A field is not empty
checked
A checkbox field is checked
email
A field is a valid e-mail address
url
A field is a url
integer
A field is an integer value or matches an integer range*
decimal
A field must be a decimal number or matches a decimal range*
number
A field is any number or matches a number range*
regExp
Matches against a regular expression
creditCard
A field is a valid credit card**
contains
, doesntContain
A field (doesn't) contain text (case insensitive)
containsExactly
, doesntContainExactly
A field (doesn't) contain text (case sensitive)
is
, not
A field is (not) a value (case insensitive)
isExactly
, notExactly
A field is (not) a value (case sensitive)
minLength
, exactLength
, maxLength
A field is at least/exactly/at most a set length
match
, different
A field should (not) match the value of another validation field. Use the field ID as the value
minCount
, exactCount
, maxCount
A multiple select field contains at least/exactly/at most a set number of selections
*
For ranges, include the parameter value = "x..y"
where x
is the minimum value and y
is the maximum value.
Leave either side blank to not have a lower/upper limit
**
Include comma separated string of card providers if required e.g. value = "visa,mastercard"
# E-mail validations
field_validation("email", field_rule("email"))
#> $identifier
#> [1] "email"
#>
#> $rules
#> $rules[[1]]
#> $type
#> [1] "email"
#>
#> attr(,"class")
#> [1] "list" "field_rule"
#>
#>
#> attr(,"class")
#> [1] "list" "field_validation"
# Password validation
field_validation(
"password",
field_rule("empty"),
field_rule("minLength", value = 8),
field_rule("regExp", "Must contain at least one special character", "\\W")
)
#> $identifier
#> [1] "password"
#>
#> $rules
#> $rules[[1]]
#> $type
#> [1] "empty"
#>
#> attr(,"class")
#> [1] "list" "field_rule"
#>
#> $rules[[2]]
#> $type
#> [1] "minLength[8]"
#>
#> attr(,"class")
#> [1] "list" "field_rule"
#>
#> $rules[[3]]
#> $type
#> [1] "regExp[/\\W/]"
#>
#> $prompt
#> [1] "Must contain at least one special character"
#>
#> attr(,"class")
#> [1] "list" "field_rule"
#>
#>
#> attr(,"class")
#> [1] "list" "field_validation"