Malasorte
Malasorte

Reputation: 1173

Javascript: if text input has any value

The snippet of code below does "something" if a particular checkbox in a form is checked and a particular text input is empty.

else if (myform.mycheckbox[1].checked && myform.mytextinput.value=="")

How can I modify it so that it does something if the checkbox is checked and the text input has ANY value (the user inserted something in it).

Thank you, sorry if this is a very basic question..

Upvotes: 0

Views: 71

Answers (4)

Andrew Newby
Andrew Newby

Reputation: 5197

If I understand you correctly, this would work:

else if (myform.mycheckbox[1].checked && myform.mytextinput.value.length > 0)

That would work as long as there was at least 1 character in the input.

Upvotes: 1

vivek_nk
vivek_nk

Reputation: 1600

if (myform.mycheckbox[1].checked && myform.mytextinput.value!=="")

Upvotes: 1

kaspermoerch
kaspermoerch

Reputation: 16570

You can do this:

else if (myform.mycheckbox[1].checked && myform.mytextinput.value)

That expression would evaluate to true if the checkbox is checked and there is something in the input field.

When you do a boolean evaluation on the content of a given variable it will evaluate to false if the content is any of the following: false, ""(empty string), 0, null or undefined. Anything else will evaluate to true.

Upvotes: 1

bensonsearch
bensonsearch

Reputation: 312

You would need <> or in this case !=

else if (myform.mycheckbox[1].checked && myform.mytextinput.value!="")

Upvotes: 1

Related Questions