Created
February 28, 2019 20:30
-
-
Save graphicbeacon/bb2e0ef4fbd8e4201ddf4010e4284fcd to your computer and use it in GitHub Desktop.
Code snippet for "How to use Streams in Dart (Part 3)" article
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
<h2>Subscribe to our Newsletter</h2> | |
<form novalidate> | |
<span id="result"></span> | |
<input type="email" name="email" id="email" placeholder="Enter your email" /> | |
<span id="validation-message"></span> | |
<button>Subscribe</button> | |
</form> |
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'; | |
void main() { | |
InputElement emailField = querySelector('input#email'); | |
ButtonElement subscribeBtn = querySelector('button'); | |
var validationContainer = querySelector('#validation-message'); | |
var result = querySelector('#result'); | |
// Input validation handler | |
emailField.onChange.listen((evt) { | |
if (!emailField.value.contains('@') || !emailField.value.contains('.')) { | |
validationContainer.text = 'Email is invalid'; | |
} else { | |
validationContainer.text = 'Email is valid'; | |
} | |
}); | |
// Form submit handler | |
subscribeBtn.onClick.listen((evt) { | |
window.console.log('Form submitting...'); | |
if (emailField.value.isNotEmpty) { | |
result.text = 'You are now subscribed!'; | |
} | |
}); | |
} |
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
input { | |
display: block; | |
margin-bottom: 10px; | |
width: 250px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment