0

I want to check if the value that the user enters in a TextFormField contains at least one String in another given String.

As an example, if the given String value is 0123456789 and if the user enters Ab3 in the TextFormField, how to check if the user entered value contains at least one String in the given String?

 String allowedChar = "0123456789";
 final split = allowedChar.split('');

I tried splitting the given value like this and checked if the textEditingController.text contains the value for each splitted value of allowedChar.

if (_value.isNotEmpty &&
  textEditingController.text.contains(c)) {
    _areAlwdCharsTyped = true;
} else if (!textEditingController.text.contains(c)) {
    _areAlwdCharsTyped = false;
}

But _areAlwdCharsTyped always returns false. Could I know a way to achieve this please? Thanks in advance.

2 Answers 2

1
void main() {
  const text = 'Ab3';
  var match = RegExp(r'\d').hasMatch(text);
  
  print(match);
}

Result

true
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking time to answer my question, sir. But the thing is allowedChar can change, since it's coming from an api call. Even though the allowedChar value is 0123456789 at the time, it could change to something else.
0

I think you're close. You can iteratively check whether the input string contains any of the chars in allowedChars.

void main() {
 String allowedChar = "0123456789";
 final split = allowedChar.split('');
 final str = 'AB3';
 final str2 = 'AB';
  
 print(split.fold<bool>(false, (prev, element) => str.contains(element) || prev)); // true
 print(split.fold<bool>(false, (prev, element) => str2.contains(element) || prev)); // false
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.