Skip to content

Instantly share code, notes, and snippets.

@suzdalnitski
Last active September 3, 2018 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzdalnitski/a48f9519b7234a8dfb0e3dcddf356459 to your computer and use it in GitHub Desktop.
Save suzdalnitski/a48f9519b7234a8dfb0e3dcddf356459 to your computer and use it in GitHub Desktop.
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