Class WRegExpValidator

Direct Known Subclasses:

public class WRegExpValidator extends WValidator
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 (

Usage example:

 WLineEdit lineEdit = new WLineEdit(this);
 // an email address validator
 WRegExpValidator validator = new WRegExpValidator("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}");


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


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 Details

    • WRegExpValidator

      public WRegExpValidator()
      Sets a new regular expression validator.
    • WRegExpValidator

      public WRegExpValidator(String 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.

  • Method Details

    • setRegExp

      public void setRegExp(String pattern)
      Sets the regular expression for valid input.

      Sets the ECMAscript regular expression expr.

    • getRegExpPattern

      public String getRegExpPattern()
      Returns the regular expression for valid input.

      Returns the ECMAScript regular expression.

    • getRegExp

      public Pattern getRegExp()
      Returns the regular expression for valid input.
    • setFlags

      public void setFlags(int flags)
      Sets regular expression matching flags.
    • getFlags

      public int getFlags()
      Returns regular expression matching flags.
    • validate

      public WValidator.Result validate(String input)
      Validates the given input.

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

      validate in class WValidator
    • setInvalidNoMatchText

      public void setInvalidNoMatchText(CharSequence text)
      Sets the message to display when the input does not match.

      The default value is "Invalid input".

    • getInvalidNoMatchText

      public WString getInvalidNoMatchText()
      Returns the message displayed when the input does not match.

      See Also:
    • getJavaScriptValidate

      public String getJavaScriptValidate()
      Description copied from class: WValidator
      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.

      getJavaScriptValidate in class WValidator
      See Also: