5
I created a simple project in Dart whose intention is to validate an e-mail field when a button is clicked, showing an error message.
However, the function Regexp.hasMath is always returning false. I do not know what can be wrong.
Below follows the HTML code and then the Dart.
<!-- formulario.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Teste e-mail</title>
</head>
<body>
<div class="form">
<div>
<input type="text" id="inputEmail">
</div>
</div>
<div class="error">
<span id="errorMessage"></span>
</div>
<script type="application/dart" src="validate.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
// validate.dart
import 'dart:html';
void main(){
querySelector('#submit').onClick.listen(verifyEmail);
}
void verifyEmail(Event e) {
String email = querySelector('#inputEmail').text;
querySelector('#errorMessage').text =
isValidEmail(email) ? '' : 'E-mail inválido!';
}
bool isValidEmail(String email){
RegExp reg = new RegExp(r"^[^@]+@[^@]+\.[^@]+$");
return reg.hasMatch(email);
}
Try reading
querySelector('#inputEmail').value
instead oftext
.– Guilherme Bernal
There is no property
value
in classElement
returned by the methodquerySelector
.– ricidleiv
Oops. Maybe
(querySelector('#inputEmail') as InputElement).value
?– Guilherme Bernal
I included your solution @Guilherme. Gratefully!
– ricidleiv
In case there should be an answer instead of editing the question. I will write one with more details.
– Guilherme Bernal
I agree, however, in this case the error found has nothing to do with the question, so I edited to include the solution. But if that has nothing to do with it, post the answer.
– ricidleiv