Skip to content

Instantly share code, notes, and snippets.

@chinedufn
Last active August 29, 2015 14:25
Show Gist options
  • Save chinedufn/d6facfde50d62a725554 to your computer and use it in GitHub Desktop.
Save chinedufn/d6facfde50d62a725554 to your computer and use it in GitHub Desktop.
Functional input validation
// Somewhere
[
h('input', {
placeholder: 'card number',
oninput: function (e) {targets.handleCardNumber({number: e.srcElement.value, state: state})
}),
h('span', state.get().cardNumberError)
]
// Elsewhere
function handleCardNumber (stuff) {
state.set('cardNumber', stuff.number)
state.set('cardNumberError', getNumberError(stuff.number))
// maybe set an errors array as well ...
// maybe disable the submit button as well
}
// Another place
if (paymentObject.errorsArray) {
// not submitted
}
else {
submitToken( createToken(paymentObject) )
}
@chinedufn
Copy link
Author

Going to be implementing something like this later today. Structure might end up being a bit different (especially that submit flow) but you get the... gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment