Created with <3 with dartpad.dev.
Created
July 18, 2022 12:08
-
-
Save mannuelf/f78e80d5b09b0be127cffad9f0993020 to your computer and use it in GitHub Desktop.
email-validation-with-streams
This file contains hidden or 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>email-validation-with-streams</title> | |
<link rel="stylesheet" href="styles.css"> | |
<script type="application/dart" src="main.dart"></script> | |
</head> | |
<body> | |
<h1 id="header"></h1> | |
<input/> | |
<div style="color: red;"></div> | |
</body> | |
</html> |
This file contains hidden or 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 header = querySelector('#header'); | |
final div = querySelector('div') as DivElement; | |
final input = querySelector('input') as InputElement; | |
final validateEmail = | |
StreamTransformer.fromHandlers(handleData: (String inputValue, sink) { | |
if (inputValue.contains('@')) { | |
sink.add(inputValue); | |
} else { | |
sink.addError('Enter a valid email'); | |
} | |
}); | |
header?.text = "Enter your email"; | |
input.onInput | |
.map((dynamic event) => event.target.value) | |
.transform(validateEmail).listen((inputValue) => div.innerHtml = '', | |
onError: (err) => div.innerHtml = err); | |
} |
This file contains hidden or 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
body { | |
display: flex; | |
flex-direction: column; | |
justify-content: center; | |
align-items: center; | |
position: absolute; | |
width: 100%; | |
height: 100%; | |
} | |
h1 { | |
color: white; | |
font-family: Arial, Helvetica, sans-serif; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment