I wrote RegExp to check email for valid form:
static bool isValidEmail(String email) {
RegExp regExp = RegExp(r'[a-zA-Z0-9_. -] @[a-zA-Z0-9-] (?:\.[a-zA-Z0-9-] ) ');
return regExp.hasMatch(email);
}
And of course unit test for it. But this test has failed:
test("Two emails", () {
String email = "[email protected] [email protected]";
bool isValid = StringHelper.isValidEmail(email);
expect(isValid, false);
});
I have tested this case on regex101.com and java, so rexExp is working. For some reason RegExp in Dart split string and check first email, and then return true. Other tests with one email(no matter, valid or not) are passing. How can I fix that?
CodePudding user response:
You should add ^
and $
to search from the begging of the string until the end: ^[a-zA-Z0-9_. -] @[a-zA-Z0-9-] (?:\.[a-zA-Z0-9-] ) $
.
This regex will check the whole string (it's email or not).