Last active
September 3, 2018 19:14
-
-
Save suzdalnitski/a48f9519b7234a8dfb0e3dcddf356459 to your computer and use it in GitHub Desktop.
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
class SimpleForm extends React.Component { | |
state = { | |
firstName: "", | |
firstNameError: "", | |
}; | |
validateName = name => { | |
const regex = /[A-Za-z]{3,}/; | |
return !regex.test(name) | |
? "The name must contain at least three letters. Numbers and special characters are not allowed." | |
: ""; | |
}; | |
onFirstNameBlur = () => { | |
const { firstName } = this.state; | |
const firstNameError = this.validateName( firstName ); | |
return this.setState({ firstNameError }); | |
}; | |
onFirstNameChange = event => | |
this.setState({ | |
firstName: event.target.value | |
}); | |
render() { | |
const { firstNameError, firstName } = this.state; | |
return ( | |
<div> | |
<div> | |
<label> | |
First name: | |
<input | |
type="text" | |
name="firstName" | |
onChange={this.onFirstNameChange} | |
onBlur={this.onFirstNameBlur} | |
/> | |
{firstNameError && <div>{firstNameError}</div>} | |
</label> | |
</div> | |
<Greetings | |
firstName={firstName} | |
/> | |
</div> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment