1

Im trying to use a regex expression to validate a full name for a textfield form in flutter but i cant figure out why it isnt working.

The expression i found from here /^[a-z ,.'-]+$/i is failing for any entry i give.

The code i'm using in dart for my flutter app is:

final RegExp nameExp = new RegExp(r"/^[a-z ,.'-]+$", caseSensitive: false);

Can anyone see what i'm missing?

Update: so the regex was allowing most things and wasnt quite correct, what worked for me was r"^([a-zA-Z]{2,}\s[a-zA-z]{1,}'?-?[a-zA-Z]{2,}\s?([a-zA-Z]{1,})?)" with the accepted answer below

Wazza
  • 1,725
  • 2
  • 17
  • 49

3 Answers3

6

Remove slash / delimiters and add the Dart delimiters : r'^[a-z ,.'-]+$ '

  • How do i include the `'` in the middle of the regex? – Wazza May 11 '18 at 16:28
  • can use \ before any special characher –  May 11 '18 at 16:31
  • ive tried this but it wont compile with `r'^[a-z ,.\'-]+$'` – Wazza May 11 '18 at 16:32
  • Don't you get an error message when it doesn't compile? – Günter Zöchbauer May 11 '18 at 16:36
  • try `r ' ^[a-z ,\.\'-]+$' ` if not work please write some name target with you to test it –  May 11 '18 at 16:38
  • So the IDE looks like https://i.imgur.com/qnUhpNi.png, and i get the errors, `Unterminated string literal.` `Expected to find ')'.` – Wazza May 11 '18 at 16:43
  • 2
    RegExp namex = new RegExp ( r"^[a-z ,.\'-]+$") ; –  May 11 '18 at 16:51
  • This stops the error but now it just allows anything but an empty string – Wazza May 11 '18 at 17:16
  • Can you give us fail/success case tests? –  May 11 '18 at 17:20
  • currently typing nothing fails, and `w` `www` `www www` `w w` all pass – Wazza May 11 '18 at 17:21
  • can add success case test of Full name example –  May 11 '18 at 17:26
  • ok so the regex was wrong but you solved my initial issues so thanks, the regex that worked was `r"^([a-zA-Z]{2,}\s[a-zA-z]{1,}'?-?[a-zA-Z]{2,}\s?([a-zA-Z]{1,})?)"` if you want your answer to be absolutely correct, i will upvote and say is correct either way – Wazza May 11 '18 at 17:38
  • @Wazza `A-z` must be changed to `A-Z`, [or it will match `_`, `\``, and some more non-letters](https://stackoverflow.com/questions/29771901/why-is-this-regex-allowing-a-caret/29771926#29771926). – Wiktor Stribiżew May 11 '18 at 17:39
5

use below method for validate full name

String fullNameValidate(String fullName) {
  String patttern = r'^[a-z A-Z,.\-]+$';
  RegExp regExp = new RegExp(patttern);
  if (fullName.length == 0) {
    return 'Please enter full name';
  } else if (!regExp.hasMatch(fullName)) {
    return 'Please enter valid full name';
  }
  return null;
}
webaddicted
  • 1,071
  • 10
  • 23
0
Pattern namePattern = r"^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$"
RegExp nameRegex = RegExp(namePattern, caseSensitive: false);
tzot
  • 92,761
  • 29
  • 141
  • 204
  • Thanks for answering; please make sure you read (or at least peruse) https://stackoverflow.com/help/formatting so that your answers stand out better. – tzot Oct 23 '20 at 12:51
  • Welcome. Thanks for your contribution. Consider adding an explanation to improve quality, and long term value. SO discourages code only Answers. Regex solutions are particularly dense and difficult to understand without explanation, especially for newcomers to regex. – SherylHohman Oct 23 '20 at 23:20