Regex for a name and number dart Regex for a name and number dart dart dart

Regex for a name and number dart


Create a static final field for the RegExp to avoid creating a new instance every time a value is checked. Creating a RegExp is expensive.

static final RegExp nameRegExp = RegExp('[a-zA-Z]');     // or RegExp(r'\p{L}'); // see https://stackoverflow.com/questions/3617797/regex-to-match-only-letters static final RegExp numberRegExp = RegExp(r'\d');

Then use it like

validator: (value) => value.isEmpty     ? 'Enter Your Name'    : (nameRegExp.hasMatch(value)         ? null         : 'Enter a Valid Name');


It seems to me you want

RegExp(r'[!@#<>?":_`~;[\]\\|=+)(*&^%0-9-]').hasMatch(value)

Note that you need to use a raw string literal, put - at the end and escape ] and \ chars inside the resulting character class, then check if there is a match with .hasMatch(value). Notre also that [0123456789] is equal to [0-9].

As for the second pattern, you can remove the digit range from the regex (as you need to allow it) and add a \s pattern (\s matches any whitespace char) to disallow whitespace in the input:

RegExp(r'[!@#<>?":_`~;[\]\\|=+)(*&^%\s-]').hasMatch(value)


Base on this answer and this information:

\p{L} or \p{Letter}: any kind of letter from any language.

Reference: http://www.regular-expressions.info/unicode.html

The regex for a name validation:

RegExp(r"^[\p{L} ,.'-]*$",      caseSensitive: false, unicode: true, dotAll: true)  .hasMatch(my_name)