I use a lightweight weighing solution and it is pretty customizable.
The input is confirmed on onChange , but can be changed to any. We can create a similar component for textarea, checkbox, radio
var Input = React.createClass({ getInitialState: function(){
Our component where we will display the form. We can add as many validation rules as you want.
var App = React.createClass({ getInitialState: function(){ return {email: ""}; }, handleChange: function(e){ this.setState({ email: e.target.value }) }, validate: function(state){ return { email: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([az]{2,6}(?:\.[az]{2})?)$/.test(state.email) } }, render: function(){ var valid = this.validate(this.state); return ( <div> <Input valid={valid.email} value={this.state.email} onChange={this.handleChange} placeholder=" example@example.com "/> </div> ); } });
It is quite simple and customizable, and works very well for small projects. But if we are a large project and have many different forms, writing every time an application component with handlers is not the best choice
source share