Created
September 23, 2020 01:42
-
-
Save GitLeandroHub/1805981ad3c65c89e3356ac26b4dbabd to your computer and use it in GitHub Desktop.
Dart Learning: Simple "E-mail" validator with Stream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h4>Enter your email</h4> | |
<input /> | |
<div style="color: red;"></div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:html'; | |
import 'dart:async'; | |
void main() { | |
final InputElement input = querySelector('input'); | |
final DivElement div = querySelector('div'); | |
final validateEmail = new StreamTransformer.fromHandlers( | |
handleData: (inputValue, sink) { | |
if (inputValue.contains('@')) { | |
sink.add(inputValue); | |
} else { | |
sink.addError('Enter a valid email'); | |
} | |
} | |
); | |
input.onInput | |
.map((dynamic event) => event.target.value) | |
.transform(validateEmail) | |
.listen( | |
(inputValue) => div.innerHtml = '', | |
onError: (err) => div.innerHtml = err | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment