Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code snippet for "How to use Streams in Dart (Part 3)" article
<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>
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!';
}
});
}
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
You can’t perform that action at this time.