简体   繁体   中英

Java Script validation

Am validating a form for empty check.

All fields works fine.

I have a dropdown when i select some value from dropdown some fields will be disabled. onchanging the dropdown value someother fields will be disabled.

Now am struck in validating the fields which are getting disabled and enabled.

if((document.form1.varAuctionTime.disabled = false) && (document.form1.varAuctionTime.value == ""))   

I used above code but it is enabling the fields.

can anybody help me out.

You are using = (assignment) where you want == (comparison)

I upvoted Quentin's answer.

document.form1.varAuctionTime.disabled = false uses the assignment operator, which sets the value of disabled to false

document.form1.varAuctionTime.disabled == false will do a comparison and will return true if the value of disabled is false (or technically, if the value is 0 or an empty string)

document.form1.varAuctionTime.disabled === false will only return true if the value is false and not if it is 0 or an empty string. This is probably not required as AFAIK the disabled property will always return a boolean.

To give you a few alternatives that you may prefer; since the comparison operators return booleans, and the disabled property is a boolean anyway, you could just do the following

if(!document.form1.varAuctionTime.disabled && !document.form1.varAuctionTime.value)

replace

document.form1.varAuctionTime.disabled = false

with

document.form1.varAuctionTime.disabled == false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM