Skip to content Skip to sidebar Skip to footer

Perform Validation When Button Is Clicked In Reactjs

When the button is clicked the validation should be checked if validation is complete then only the form data should go to database. I want it like this https://codesandbox.io/s/7r

Solution 1:

So there are a few things that has to be fixed here in order to make it work. But we will have it done in no time

#1 The for each error

First of all you are calling the forEach on the object (newUser) before you have initialized it. Not having defined a variable leads to it being... undefined. Ergo the error

Cannot read property 'forEach' of undefined thanks in advance

However, even if we were to fix that and swapped the place of the 2, it still would not work. You are calling the function forEach that is a property of the array object in javascript when in fact your newUser is a js object. In order for you to loop over the keys in your object you are going to want to default to a normal for loop looking like this

for(var key in newUser){
  isValid = this.validateField(key) && isValid;
}

Hopefully this fixes your problem

Post a Comment for "Perform Validation When Button Is Clicked In Reactjs"