Class WRegExpValidator
- Direct Known Subclasses:
WTimeValidator
public class WRegExpValidator extends WValidator
This validator checks whether user input matches the given (perl-like) regular expression. It checks the complete input; prefix ^ and suffix $ are not needed.
The following perl features are not supported (since client-side validation cannot handle them):
- No Lookbehind support, i.e. the constructs (?<=text) and (?<!text).
- No atomic grouping, i.e. the construct (?>group).
- No conditional expressions, i.e. the consturct (?ifthen|else).
See http://www.boost.org/doc/libs/release/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html for a full overview of the supported regular expression syntax. However, if you want client-side validation to work correctly, you will have to limit your regular expressions to those features supported by JavaScript (ECMAScript style regular expressions). See http://en.cppreference.com/w/cpp/regex/ecmascript for an overview of the ECMAScript regular expression 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}");
lineEdit.setValidator(validator);
lineEdit.setText("pieter@emweb.be");
Note: This validator does not fully support unicode: it matches on the 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
-
Nested Class Summary
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WValidator
WValidator.Result, WValidator.State
Nested classes/interfaces inherited from class eu.webtoolkit.jwt.WObject
WObject.FormData
-
Constructor Summary
Constructors Constructor Description WRegExpValidator()
Sets a new regular expression validator.WRegExpValidator(WObject parent)
Sets a new regular expression validator.WRegExpValidator(java.lang.String pattern)
Sets a new regular expression validator that accepts input that matches the given regular expression.WRegExpValidator(java.lang.String pattern, WObject parent)
Sets a new regular expression validator that accepts input that matches the given regular expression. -
Method Summary
Modifier and Type Method Description int
getFlags()
Returns regular expression matching flags.WString
getInvalidNoMatchText()
Returns the message displayed when the input does not match.java.lang.String
getJavaScriptValidate()
Creates a Javascript object that validates the input.java.lang.String
getRegExp()
Returns the regular expression for valid input.void
setFlags(int flags)
Sets regular expression matching flags.void
setInvalidNoMatchText(java.lang.CharSequence text)
Sets the message to display when the input does not match.void
setNoMatchText(java.lang.CharSequence text)
Deprecated.void
setRegExp(java.lang.String pattern)
Sets the regular expression for valid input.WValidator.Result
validate(java.lang.String input)
Validates the given input.Methods inherited from class eu.webtoolkit.jwt.WValidator
getFormat, getInputFilter, getInvalidBlankText, isMandatory, setInvalidBlankText, setMandatory
Methods inherited from class eu.webtoolkit.jwt.WObject
addChild, getId, getObjectName, remove, setFormData, setObjectName, tr
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Constructor Details
-
WRegExpValidator
Sets a new regular expression validator. -
WRegExpValidator
public WRegExpValidator()Sets a new regular expression validator.Calls
this((WObject)null)
-
WRegExpValidator
Sets a new regular expression validator that accepts input that matches the given regular expression.This constructs a validator that matches the perl regular expression
expr
. -
WRegExpValidator
public WRegExpValidator(java.lang.String pattern)Sets a new regular expression validator that accepts input that matches the given regular expression.
-
-
Method Details
-
setRegExp
public void setRegExp(java.lang.String pattern)Sets the regular expression for valid input.Sets the perl regular expression
expr
. -
getRegExp
public java.lang.String getRegExp()Returns the regular expression for valid input.Returns the perl regular expression.
-
setFlags
public void setFlags(int flags)Sets regular expression matching flags. -
getFlags
public int getFlags()Returns regular expression matching flags. -
validate
Validates the given input.The input is considered valid only when it is blank for a non-mandatory field, or matches the regular expression.
- Overrides:
validate
in classWValidator
-
setNoMatchText
public void setNoMatchText(java.lang.CharSequence text)Deprecated.UsesetInvalidNoMatchText()
insteadSets the text to be shown if no match can be found.This calls
setInvalidNoMatchText()
-
setInvalidNoMatchText
public void setInvalidNoMatchText(java.lang.CharSequence text)Sets the message to display when the input does not match.The default value is "Invalid input".
-
getInvalidNoMatchText
Returns the message displayed when the input does not match.- See Also:
setInvalidNoMatchText(CharSequence text)
-
getJavaScriptValidate
public java.lang.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.
Note: The signature and contract changed changed in JWt 3.1.9.
- Overrides:
getJavaScriptValidate
in classWValidator
- See Also:
WValidator.getInputFilter()
-
setInvalidNoMatchText()
instead