Home > Software design >  how to let validator for textfield allow other language like german russian or arabic, flutter
how to let validator for textfield allow other language like german russian or arabic, flutter

Time:08-26

Hi here is the validator for textfield, but I assumed my user is non english speaker, so is there a way to include other language:

  validateFolderName(value) {
    if (value == null || value.isEmpty) {
      return 'sorry, name could not be empty';
    } else if (!InputValidators.nameValidate(value)) {
      return 'only alphabets and numbers only';
    } else if (value.length >= 20) {
      return 'sorry, name could not be too long';
    }
    return null;
  }

class InputValidators {
  static bool nameValidate(String value) {
    return RegExp(r'^[a-zA-Z0-9] $').hasMatch(value);
  }
}

thank you for any clues!

CodePudding user response:

You can try this regex

var reg = RegExp(r"^(?:\p{L}\p{Mn}*|\p{N}) $", unicode: true);

This will include all letters, numbers and diacritics and other Unicode too

CodePudding user response:

Dart regular expressions have the same syntax and semantics as JavaScript regular expressions. It's regulating by ecma-international.org/ecma-262/9.0/#sec-regexp-regular-expression-objects

To validate not only English letters you need:

[^\x00-\x7F]

this is for non English letters plus

\w

for English. All together:

([^\x00-\x7F]|\w) 

Abd for Unicode:

([^\u0000-\u007F]|\w) 
  • Related