Simplest way to validate forms in react
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm quite unsure how to do a form validation within react.
I want to display an error message on the current page saying. However, the password validation is ignored so their is no error that shows on the page.
Password must be at least characters
Maybe i'm not using conditional rendering right
SignUp.js (snippet for demonstration purpose)
constructor(props){
super(props);
this.state ={
errors: {},
}
handleSubmit(event) {
event.preventDefault();
const email = this.email.value;
const password = this.password.value;
if(password.length > 6){
this.state.errors.password= "Password must be at least 6 characters";
}
const creds = {email, password}
if(creds){
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
ref={(input) => this.email = input}
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.password > 6 &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
ref={(input) => this.password = input}
value={this.state.password}
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
javascript reactjs redux
add a comment |
I'm quite unsure how to do a form validation within react.
I want to display an error message on the current page saying. However, the password validation is ignored so their is no error that shows on the page.
Password must be at least characters
Maybe i'm not using conditional rendering right
SignUp.js (snippet for demonstration purpose)
constructor(props){
super(props);
this.state ={
errors: {},
}
handleSubmit(event) {
event.preventDefault();
const email = this.email.value;
const password = this.password.value;
if(password.length > 6){
this.state.errors.password= "Password must be at least 6 characters";
}
const creds = {email, password}
if(creds){
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
ref={(input) => this.email = input}
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.password > 6 &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
ref={(input) => this.password = input}
value={this.state.password}
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
javascript reactjs redux
It looks like you need to changethis.password > 6
tothis.state.password.length < 6
– ic3b3rg
Jan 4 at 2:55
im gettingTypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 2:58
add a comment |
I'm quite unsure how to do a form validation within react.
I want to display an error message on the current page saying. However, the password validation is ignored so their is no error that shows on the page.
Password must be at least characters
Maybe i'm not using conditional rendering right
SignUp.js (snippet for demonstration purpose)
constructor(props){
super(props);
this.state ={
errors: {},
}
handleSubmit(event) {
event.preventDefault();
const email = this.email.value;
const password = this.password.value;
if(password.length > 6){
this.state.errors.password= "Password must be at least 6 characters";
}
const creds = {email, password}
if(creds){
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
ref={(input) => this.email = input}
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.password > 6 &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
ref={(input) => this.password = input}
value={this.state.password}
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
javascript reactjs redux
I'm quite unsure how to do a form validation within react.
I want to display an error message on the current page saying. However, the password validation is ignored so their is no error that shows on the page.
Password must be at least characters
Maybe i'm not using conditional rendering right
SignUp.js (snippet for demonstration purpose)
constructor(props){
super(props);
this.state ={
errors: {},
}
handleSubmit(event) {
event.preventDefault();
const email = this.email.value;
const password = this.password.value;
if(password.length > 6){
this.state.errors.password= "Password must be at least 6 characters";
}
const creds = {email, password}
if(creds){
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
ref={(input) => this.email = input}
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.password > 6 &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
ref={(input) => this.password = input}
value={this.state.password}
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
javascript reactjs redux
javascript reactjs redux
asked Jan 4 at 2:52
Eric ThomasEric Thomas
63
63
It looks like you need to changethis.password > 6
tothis.state.password.length < 6
– ic3b3rg
Jan 4 at 2:55
im gettingTypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 2:58
add a comment |
It looks like you need to changethis.password > 6
tothis.state.password.length < 6
– ic3b3rg
Jan 4 at 2:55
im gettingTypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 2:58
It looks like you need to change
this.password > 6
to this.state.password.length < 6
– ic3b3rg
Jan 4 at 2:55
It looks like you need to change
this.password > 6
to this.state.password.length < 6
– ic3b3rg
Jan 4 at 2:55
im getting
TypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 2:58
im getting
TypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 2:58
add a comment |
3 Answers
3
active
oldest
votes
You should store the form data in your component's state. For example, have this.state.email
instead of this.email
. When data that is stored in a component's state is updated, a rerender is triggered. However this rerender behavior is not the case for plain class variables. You also directly manipulate the state when you set the errors
. Instead you should use setState
docs.
The reason you do not see your error shown on the page is because your page is not properly rerendering due to changes in the form.
Note it is also a good idea to wrap your form data variables in a formData
object within state for organization. This helps keep the form's data separate from the rest of the component's state and allows the form data to be passed around more easily.
Here is an example of how you can reorganize things:
constructor(props){
super(props);
this.state = {
formData: { // set up default form values
email: "",
password: "",
},
errors: {},
}
handleChange = event => {
const { formData } = this.state;
this.setState({
formData: {
...formData, // leave other values unchanged
[event.target.name]: event.target.value, // update the changed value
}
});
}
handleSubmit(event) {
event.preventDefault();
const { formData, errors } = this.state;
const { email, password } = formData;
if (password.length > 6) {
this.setState({ // update errors using setState -- never directly modify a component's state
errors: {
...errors,
password: "Password must be at least 6 characters",
}
});
}
const creds = {email, password}
if (creds) {
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
value={ this.state.formData.email } {/* control component by storing value in state and updating state when the input changes */}
onChange={ this.handleChange }
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
value={ this.state.formData.password }
onChange={ this.handleChange }
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
thank you so much henry, i understand now.
– Eric Thomas
Jan 4 at 3:33
the only this, the{this.state.formData.password.length < 6 && //display an error here <h2>{this.state.errors.password}</h2> }
doesn't display anything
– Eric Thomas
Jan 4 at 3:37
but it appears that it wants to show it self, it looks invisible. i can see the div move slightly down more to make space for the error. but no error appears.
– Eric Thomas
Jan 4 at 3:38
@EricThomas could be a CSS issue, if you disable CSS or open your browser's developer tools can you see the message? (I've also updated that section, because checking the password length is duplicate logic, it would be enough to just check if there is an error present for the password)
– Henry Woody
Jan 4 at 3:41
add a comment |
This example isn't using the component state to manage the Password and Email fields.
By having them as controlled input fields you can then validate them on change and conditionally render as error message (which doesn't need to be held in state)
eg. if you wanted to show an error is the password is too short:
{ this.state.password.length < 5 && <p>Password too short</p> }
Maybe check out https://github.com/jaredpalmer/formik
and look at some examples.
im getting this errorTypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 3:02
add a comment |
I think your code to show error show use state
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
And in submit form you should update your state
if(password.length > 6){
this.setState({ errors: { password: "Password must be at least 6 characters"}
})
} else {
this.setState({errors: {}})
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032554%2fsimplest-way-to-validate-forms-in-react%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should store the form data in your component's state. For example, have this.state.email
instead of this.email
. When data that is stored in a component's state is updated, a rerender is triggered. However this rerender behavior is not the case for plain class variables. You also directly manipulate the state when you set the errors
. Instead you should use setState
docs.
The reason you do not see your error shown on the page is because your page is not properly rerendering due to changes in the form.
Note it is also a good idea to wrap your form data variables in a formData
object within state for organization. This helps keep the form's data separate from the rest of the component's state and allows the form data to be passed around more easily.
Here is an example of how you can reorganize things:
constructor(props){
super(props);
this.state = {
formData: { // set up default form values
email: "",
password: "",
},
errors: {},
}
handleChange = event => {
const { formData } = this.state;
this.setState({
formData: {
...formData, // leave other values unchanged
[event.target.name]: event.target.value, // update the changed value
}
});
}
handleSubmit(event) {
event.preventDefault();
const { formData, errors } = this.state;
const { email, password } = formData;
if (password.length > 6) {
this.setState({ // update errors using setState -- never directly modify a component's state
errors: {
...errors,
password: "Password must be at least 6 characters",
}
});
}
const creds = {email, password}
if (creds) {
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
value={ this.state.formData.email } {/* control component by storing value in state and updating state when the input changes */}
onChange={ this.handleChange }
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
value={ this.state.formData.password }
onChange={ this.handleChange }
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
thank you so much henry, i understand now.
– Eric Thomas
Jan 4 at 3:33
the only this, the{this.state.formData.password.length < 6 && //display an error here <h2>{this.state.errors.password}</h2> }
doesn't display anything
– Eric Thomas
Jan 4 at 3:37
but it appears that it wants to show it self, it looks invisible. i can see the div move slightly down more to make space for the error. but no error appears.
– Eric Thomas
Jan 4 at 3:38
@EricThomas could be a CSS issue, if you disable CSS or open your browser's developer tools can you see the message? (I've also updated that section, because checking the password length is duplicate logic, it would be enough to just check if there is an error present for the password)
– Henry Woody
Jan 4 at 3:41
add a comment |
You should store the form data in your component's state. For example, have this.state.email
instead of this.email
. When data that is stored in a component's state is updated, a rerender is triggered. However this rerender behavior is not the case for plain class variables. You also directly manipulate the state when you set the errors
. Instead you should use setState
docs.
The reason you do not see your error shown on the page is because your page is not properly rerendering due to changes in the form.
Note it is also a good idea to wrap your form data variables in a formData
object within state for organization. This helps keep the form's data separate from the rest of the component's state and allows the form data to be passed around more easily.
Here is an example of how you can reorganize things:
constructor(props){
super(props);
this.state = {
formData: { // set up default form values
email: "",
password: "",
},
errors: {},
}
handleChange = event => {
const { formData } = this.state;
this.setState({
formData: {
...formData, // leave other values unchanged
[event.target.name]: event.target.value, // update the changed value
}
});
}
handleSubmit(event) {
event.preventDefault();
const { formData, errors } = this.state;
const { email, password } = formData;
if (password.length > 6) {
this.setState({ // update errors using setState -- never directly modify a component's state
errors: {
...errors,
password: "Password must be at least 6 characters",
}
});
}
const creds = {email, password}
if (creds) {
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
value={ this.state.formData.email } {/* control component by storing value in state and updating state when the input changes */}
onChange={ this.handleChange }
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
value={ this.state.formData.password }
onChange={ this.handleChange }
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
thank you so much henry, i understand now.
– Eric Thomas
Jan 4 at 3:33
the only this, the{this.state.formData.password.length < 6 && //display an error here <h2>{this.state.errors.password}</h2> }
doesn't display anything
– Eric Thomas
Jan 4 at 3:37
but it appears that it wants to show it self, it looks invisible. i can see the div move slightly down more to make space for the error. but no error appears.
– Eric Thomas
Jan 4 at 3:38
@EricThomas could be a CSS issue, if you disable CSS or open your browser's developer tools can you see the message? (I've also updated that section, because checking the password length is duplicate logic, it would be enough to just check if there is an error present for the password)
– Henry Woody
Jan 4 at 3:41
add a comment |
You should store the form data in your component's state. For example, have this.state.email
instead of this.email
. When data that is stored in a component's state is updated, a rerender is triggered. However this rerender behavior is not the case for plain class variables. You also directly manipulate the state when you set the errors
. Instead you should use setState
docs.
The reason you do not see your error shown on the page is because your page is not properly rerendering due to changes in the form.
Note it is also a good idea to wrap your form data variables in a formData
object within state for organization. This helps keep the form's data separate from the rest of the component's state and allows the form data to be passed around more easily.
Here is an example of how you can reorganize things:
constructor(props){
super(props);
this.state = {
formData: { // set up default form values
email: "",
password: "",
},
errors: {},
}
handleChange = event => {
const { formData } = this.state;
this.setState({
formData: {
...formData, // leave other values unchanged
[event.target.name]: event.target.value, // update the changed value
}
});
}
handleSubmit(event) {
event.preventDefault();
const { formData, errors } = this.state;
const { email, password } = formData;
if (password.length > 6) {
this.setState({ // update errors using setState -- never directly modify a component's state
errors: {
...errors,
password: "Password must be at least 6 characters",
}
});
}
const creds = {email, password}
if (creds) {
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
value={ this.state.formData.email } {/* control component by storing value in state and updating state when the input changes */}
onChange={ this.handleChange }
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
value={ this.state.formData.password }
onChange={ this.handleChange }
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
You should store the form data in your component's state. For example, have this.state.email
instead of this.email
. When data that is stored in a component's state is updated, a rerender is triggered. However this rerender behavior is not the case for plain class variables. You also directly manipulate the state when you set the errors
. Instead you should use setState
docs.
The reason you do not see your error shown on the page is because your page is not properly rerendering due to changes in the form.
Note it is also a good idea to wrap your form data variables in a formData
object within state for organization. This helps keep the form's data separate from the rest of the component's state and allows the form data to be passed around more easily.
Here is an example of how you can reorganize things:
constructor(props){
super(props);
this.state = {
formData: { // set up default form values
email: "",
password: "",
},
errors: {},
}
handleChange = event => {
const { formData } = this.state;
this.setState({
formData: {
...formData, // leave other values unchanged
[event.target.name]: event.target.value, // update the changed value
}
});
}
handleSubmit(event) {
event.preventDefault();
const { formData, errors } = this.state;
const { email, password } = formData;
if (password.length > 6) {
this.setState({ // update errors using setState -- never directly modify a component's state
errors: {
...errors,
password: "Password must be at least 6 characters",
}
});
}
const creds = {email, password}
if (creds) {
this.props.signUp(creds);
this.props.history.push('/');
}
}
render() {
return (
<div className="container">
<div className="row">
<div className="col-md-6">
<h1>Sign Up</h1>
<form onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="exampleInputEmail1">Email address</label>
<input
name="email"
type="email"
className="form-control"
id="email"
value={ this.state.formData.email } {/* control component by storing value in state and updating state when the input changes */}
onChange={ this.handleChange }
aria-describedby="emailHelp"
placeholder="Enter email" />
<small id="emailHelp" className="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div className="form-group">
<label htmlFor="exampleInputPassword1">Password</label>
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
<input
name="password"
type="password"
value={ this.state.formData.password }
onChange={ this.handleChange }
className="form-control"
id="password"
placeholder="Password" />
</div>
<button type="submit" className="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
);
}
}
edited Jan 4 at 20:20
answered Jan 4 at 3:07
Henry WoodyHenry Woody
5,06531127
5,06531127
thank you so much henry, i understand now.
– Eric Thomas
Jan 4 at 3:33
the only this, the{this.state.formData.password.length < 6 && //display an error here <h2>{this.state.errors.password}</h2> }
doesn't display anything
– Eric Thomas
Jan 4 at 3:37
but it appears that it wants to show it self, it looks invisible. i can see the div move slightly down more to make space for the error. but no error appears.
– Eric Thomas
Jan 4 at 3:38
@EricThomas could be a CSS issue, if you disable CSS or open your browser's developer tools can you see the message? (I've also updated that section, because checking the password length is duplicate logic, it would be enough to just check if there is an error present for the password)
– Henry Woody
Jan 4 at 3:41
add a comment |
thank you so much henry, i understand now.
– Eric Thomas
Jan 4 at 3:33
the only this, the{this.state.formData.password.length < 6 && //display an error here <h2>{this.state.errors.password}</h2> }
doesn't display anything
– Eric Thomas
Jan 4 at 3:37
but it appears that it wants to show it self, it looks invisible. i can see the div move slightly down more to make space for the error. but no error appears.
– Eric Thomas
Jan 4 at 3:38
@EricThomas could be a CSS issue, if you disable CSS or open your browser's developer tools can you see the message? (I've also updated that section, because checking the password length is duplicate logic, it would be enough to just check if there is an error present for the password)
– Henry Woody
Jan 4 at 3:41
thank you so much henry, i understand now.
– Eric Thomas
Jan 4 at 3:33
thank you so much henry, i understand now.
– Eric Thomas
Jan 4 at 3:33
the only this, the
{this.state.formData.password.length < 6 && //display an error here <h2>{this.state.errors.password}</h2> }
doesn't display anything– Eric Thomas
Jan 4 at 3:37
the only this, the
{this.state.formData.password.length < 6 && //display an error here <h2>{this.state.errors.password}</h2> }
doesn't display anything– Eric Thomas
Jan 4 at 3:37
but it appears that it wants to show it self, it looks invisible. i can see the div move slightly down more to make space for the error. but no error appears.
– Eric Thomas
Jan 4 at 3:38
but it appears that it wants to show it self, it looks invisible. i can see the div move slightly down more to make space for the error. but no error appears.
– Eric Thomas
Jan 4 at 3:38
@EricThomas could be a CSS issue, if you disable CSS or open your browser's developer tools can you see the message? (I've also updated that section, because checking the password length is duplicate logic, it would be enough to just check if there is an error present for the password)
– Henry Woody
Jan 4 at 3:41
@EricThomas could be a CSS issue, if you disable CSS or open your browser's developer tools can you see the message? (I've also updated that section, because checking the password length is duplicate logic, it would be enough to just check if there is an error present for the password)
– Henry Woody
Jan 4 at 3:41
add a comment |
This example isn't using the component state to manage the Password and Email fields.
By having them as controlled input fields you can then validate them on change and conditionally render as error message (which doesn't need to be held in state)
eg. if you wanted to show an error is the password is too short:
{ this.state.password.length < 5 && <p>Password too short</p> }
Maybe check out https://github.com/jaredpalmer/formik
and look at some examples.
im getting this errorTypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 3:02
add a comment |
This example isn't using the component state to manage the Password and Email fields.
By having them as controlled input fields you can then validate them on change and conditionally render as error message (which doesn't need to be held in state)
eg. if you wanted to show an error is the password is too short:
{ this.state.password.length < 5 && <p>Password too short</p> }
Maybe check out https://github.com/jaredpalmer/formik
and look at some examples.
im getting this errorTypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 3:02
add a comment |
This example isn't using the component state to manage the Password and Email fields.
By having them as controlled input fields you can then validate them on change and conditionally render as error message (which doesn't need to be held in state)
eg. if you wanted to show an error is the password is too short:
{ this.state.password.length < 5 && <p>Password too short</p> }
Maybe check out https://github.com/jaredpalmer/formik
and look at some examples.
This example isn't using the component state to manage the Password and Email fields.
By having them as controlled input fields you can then validate them on change and conditionally render as error message (which doesn't need to be held in state)
eg. if you wanted to show an error is the password is too short:
{ this.state.password.length < 5 && <p>Password too short</p> }
Maybe check out https://github.com/jaredpalmer/formik
and look at some examples.
answered Jan 4 at 3:01
Louis van SendenLouis van Senden
153
153
im getting this errorTypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 3:02
add a comment |
im getting this errorTypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 3:02
im getting this error
TypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 3:02
im getting this error
TypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 3:02
add a comment |
I think your code to show error show use state
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
And in submit form you should update your state
if(password.length > 6){
this.setState({ errors: { password: "Password must be at least 6 characters"}
})
} else {
this.setState({errors: {}})
}
add a comment |
I think your code to show error show use state
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
And in submit form you should update your state
if(password.length > 6){
this.setState({ errors: { password: "Password must be at least 6 characters"}
})
} else {
this.setState({errors: {}})
}
add a comment |
I think your code to show error show use state
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
And in submit form you should update your state
if(password.length > 6){
this.setState({ errors: { password: "Password must be at least 6 characters"}
})
} else {
this.setState({errors: {}})
}
I think your code to show error show use state
{this.state.errors.password &&
//display an error here
<h2>{this.state.errors.password}</h2>
}
And in submit form you should update your state
if(password.length > 6){
this.setState({ errors: { password: "Password must be at least 6 characters"}
})
} else {
this.setState({errors: {}})
}
answered Jan 4 at 8:43
duc maiduc mai
46037
46037
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032554%2fsimplest-way-to-validate-forms-in-react%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It looks like you need to change
this.password > 6
tothis.state.password.length < 6
– ic3b3rg
Jan 4 at 2:55
im getting
TypeError: Cannot read property 'length' of undefined
– Eric Thomas
Jan 4 at 2:58