Jump to content

Extension:Page Forms/Input types/Regexp

From mediawiki.org

With the input type regexp you can fine-tune what values are allowed and what is blocked in your input fields. You can even string several filters together to have a cascade of checks, e.g. to give an error message specific to what violation occurred.

The special parameters for this input type are:

  • regexp - a JavaScript regular expression, which the user input has to match. It must include slashes, at the beginning and end. See Regular Expressions for an introduction to writing a regular expression pattern in JavaScript, and Regex Pal for an interactive testing tool. Note that for the regexp, you cannot use the | character because it gets lost in the field definition. See "or char", below.
  • base type - the actual input type to display. By default, it is "text", but it can be any other input type as well.
  • inverse - if set, the input must NOT match the regular expression in order to be valid.
  • message= - a custom error message to display, when the user input does not pass the validation. Defaults to Wrong format! (or its equivalent in the current locale).
  • or char= - the character that can be used in the regular expression instead of |. Defaults to !.
  • base prefix= - lets you set more than one regexp for this input; see below.

If you want to specify more than one regexp filter, you need to specify a prefix for each filter in the parameter base prefix of its predecessor. Sounds complicated? Have a look at the example below. You may, but do not need to specify a prefix for the final input type - each filter stage consumes only the parameters from the parameter set, that it understands, i.e. the specific parameters listed below.

Examples

[edit]

A simple text input field accepting only letters, numbers and spaces. (This may be useful for fields from which an article's name is generated in the Page Forms one-step process.)

{{{field|foo|input type=regexp|regexp=/^[0-9A-Za-z ]+$/}}}

A text input field accepting only numbers and having no more than 5 digits. Certain numbers are forbidden. Each condition gets its own error message.

{{{field|foo|input type=regexp
|regexp=/^\d*$/|message=Only numbers!|base type=regexp|base prefix=filter2
|filter2.regexp=/^.{0,5}$/|filter2.message=No more than 5 digits!|filter2.base type=regexp|filter2.base prefix=filter3
|filter3.regexp=/^666$/|filter3.message=Don't you dare!|filter3.inverse}}}