Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Handling undefined text input value

I have this condition here that checks if the value is undefined

console.log("Value: " + typeof getValues('departmentId'))
if (String(getValues('departmentId')) != "" ||
    String(getValues('departmentDesc')) != "" ||
    typeof getValues('departmentId') !== undefined ||
    typeof getValues('departmentDesc') !== undefined){
    dispatch(setIsConfirm(true))
    console.log("Clearing fields || Value: " + getValues('departmentId'))
}

But for some reason it still ran even though it is undefined, here is the picture of the
console

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

typeof returns a string. So you need to do this instead:

console.log("Value: " + typeof getValues('departmentId'))
if (typeof getValues('departmentId') !== "undefined" || typeof getValues('departmentDesc') !== 'undefined'){
    dispatch(setIsConfirm(true))
    console.log("Clearing fields || Value: " + getValues('departmentId'))
}

Also, make sure you really need OR || operator and not AND &&. Replace it if needed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading