validator_base_format (zotonic_mod_base v1.0.0-rc.17)

Regular expression test.

Checks if an input element’s value matches a regular expression. There is an optional negate argument to validate only when the regular expression does not match.

For example, to test a dutch postal code:

<input type="text" id="postcode" name="postcode" value="" />
{% validate id="postcode" type={format pattern="^[0-9][0-9][0-9][0-9] +[A-Za-z][A-Z][a-z]$"} %}

Another example, no digits allowed:

<input type="text" id="nodigit" name="nodigit" value="" />
{% validate id="nodigit" type={format pattern="[0-9]" negate} %}

Arguments

ArgumentDescriptionExample
patternThe regular expression to match against.pattern="[0-9][a-z]+"
negateSpecify negate when you want to accept values that do not match the pattern.negate
failure\_messageMessage to show when the input value does not match the pattern (or does match the pattern when the negate argument is given). Defaults to “Not valid.”failure_message="Invalid postcode"

See also

Forms and validation

Summary

Functions

render_validator/5

validate/5

-spec validate(term(), term(), term(), list(), z:context()) ->
                  {{ok, term()}, z:context()} | {{error, term(), term()}, z:context()}.