Wt: Wt::WRegExpValidator Class Reference

Wt 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | List of all members
Wt::WRegExpValidator Class Reference

A validator that checks user input against a regular expression. More...

`#include <Wt/WRegExpValidator.h>`

Inheritance diagram for Wt::WRegExpValidator:
[legend]

## Public Member Functions
| WRegExpValidator ()
| Sets a new regular expression validator.
| WRegExpValidator (const WString &pattern)
| Sets a new regular expression validator that accepts input that matches the given regular expression.
| ~WRegExpValidator ()
| Destructor.
void | setRegExp (const WString &pattern)
| Sets the regular expression for valid input.
WString | regExpPattern () const
| Returns the regular expression for valid input.
std::regex | regExp () const
| Returns the regular expression for valid input.
void | setFlags (WFlags< RegExpFlag > flags)
| Sets regular expression matching flags.
WFlags< RegExpFlag > | flags () const
| Returns regular expression matching flags.
virtual Result | validate (const WString &input) const override
| Validates the given input.
void | setInvalidNoMatchText (const WString &text)
| Sets the message to display when the input does not match.
WString | invalidNoMatchText () const
| Returns the message displayed when the input does not match.
virtual std::string | javaScriptValidate () const override
| Creates a Javascript object that validates the input.
Public Member Functions inherited from Wt::WValidator
| WValidator (bool mandatory=false)
| Creates a new validator.
virtual | ~WValidator ()
| Destructor.
void | setMandatory (bool how)
| Sets if input is mandatory.
bool | isMandatory () const
| Returns if input is mandatory.
void | setInvalidBlankText (const WString &text)
| Sets the message to display when a mandatory field is left blank.
WString | invalidBlankText () const
| Returns the message displayed when a mandatory field is left blank.
virtual WString | format () const
| Returns the validator format.
virtual std::string | inputFilter () const
| Returns a regular expression that filters input.

## Additional Inherited Members
Public Types inherited from Wt::WValidator
typedef ValidationState | State
| Typedef for enum Wt::ValidationState.
Protected Member Functions inherited from Wt::WValidator

## Detailed Description

A validator that checks user input against a regular expression.

This validator checks whether user input matches the given regular expression. It checks the complete input; prefix ^ and suffix $ are not needed.

The regex should be specified using ECMAScript syntax (http://en.cppreference.com/w/cpp/regex/ecmascript)

Usage example:

Wt::WLineEdit *lineEdit = addWidget(std::make_unique<Wt::WLineEdit>());
// an email address validator
auto validator = std::make_unique<Wt::WRegExpValidator>("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}");
lineEdit->setValidator(validator);
lineEdit->setText("koen@emweb.be");
Wt::WFormWidget::setValidator
void setValidator(const std::shared_ptr< WValidator > &validator)
Sets a validator for this field.
Wt::WLineEdit
A widget that provides a single line edit.
Definition WLineEdit.h:76
Wt::WLineEdit::setText
virtual void setText(const WString &text)
Sets the content of the line edit.

Note
  This validator does not fully support unicode: it matches on the CharEncoding::UTF8-encoded representation of the string.

### i18n

The strings used in this class can be translated by overriding the default values for the following localization keys:

- Wt.WRegExpValidator.Invalid: Invalid input

## Constructor & Destructor Documentation

## ◆ WRegExpValidator()

Wt::WRegExpValidator::WRegExpValidator | ( | const WString & | pattern | ) |

Sets a new regular expression validator that accepts input that matches the given regular expression.

This constructs a validator that matches the regular expression `expr`.

## Member Function Documentation

## ◆ invalidNoMatchText()

WString Wt::WRegExpValidator::invalidNoMatchText | ( |  | ) | const

Returns the message displayed when the input does not match.

See also
  setInvalidNoMatchText(const WString&)

## ◆ javaScriptValidate()

virtual std::string Wt::WRegExpValidator::javaScriptValidate | ( |  | ) | const
| overridevirtual

Creates a Javascript object that validates the input.

The JavaScript expression should evaluate to an object which contains a `validate(text)` function, which returns an object that contains the following two fields:

- fields: a boolean valid,
- a `message` that indicates the problem if not valid.

Returns an empty string if the validator does not provide a client-side validation implementationq.

See also
  inputFilter()

Reimplemented from Wt::WValidator.

Reimplemented in Wt::WTimeValidator.

## ◆ regExpPattern()

WString Wt::WRegExpValidator::regExpPattern | ( |  | ) | const

Returns the regular expression for valid input.

Returns the ECMAScript regular expression.

## ◆ setInvalidNoMatchText()

void Wt::WRegExpValidator::setInvalidNoMatchText | ( | const WString & | text | ) |

Sets the message to display when the input does not match.

The default value is "Invalid input".

## ◆ setRegExp()

void Wt::WRegExpValidator::setRegExp | ( | const WString & | pattern | ) |

Sets the regular expression for valid input.

Sets the ECMAscript regular expression `expr`.

## ◆ validate()

virtual Result Wt::WRegExpValidator::validate | ( | const WString & | input | ) | const
| overridevirtual

Validates the given input.

The input is considered valid only when it is blank for a non-mandatory field, or matches the regular expression.

Reimplemented from Wt::WValidator.

Reimplemented in Wt::WTimeValidator.