Alexandra Tiganciuc
Alexandra Tiganciuc

Reputation: 19

what is wrong in this code that made for validating a input field

I have a input where the user should write ELIMINA so the modal could be submitted, so far I writed this functionality for validation :

var validation = false;
    function validateElimina(e) {
        var inputField = e.target.value;
        if (inputField === 'ELIMINA') {
            validation = true;
        } 
        validation = false;
    }

    function onValidate() {
        if(validation == true) {
            return console.log('EXISTS')
        }
        return console.log('FALSE');
    }

this is input field :

{props.eliminaInput ? <Input onChange ={(e) => validateElimina(e) } className="elimina-input" type="text" autoFocus={true} /> : null}

and this is the button for submitting :

<Button
   onClick={() => onValidate()}
   className="button-modal col-5 py-2 text-uppercase">
    {props.buttonText}
</Button>

So for now, if I'm pressing the button doesn't matter what is in input field, it's showing FALSE. Help please.

Upvotes: 0

Views: 43

Answers (2)

Vibhav
Vibhav

Reputation: 321

const [inputValue, setInputValue] = useState()
function validateElimina(e) {
    const inputField = e.target.value;
   setInputValue(inputField)
}


function onValidate() {
        if(inputValue == “ELIMINA”) {
            return console.log('EXISTS')
        }
        else
        return console.log('FALSE');
    }

Upvotes: 1

epascarello
epascarello

Reputation: 207501

You are always setting it to false. Move it before the if.

validation = false;
if (inputField === 'ELIMINA') {
  validation = true;
} 

or use an else

if (inputField === 'ELIMINA') {
  validation = true;
} else {
  validation = false;
}

Upvotes: 1

Related Questions