0

The below works, how would i go about including a 2nd "txtArea2"? I've tried joining a & (document.getElementById("txtArea2").value == '') but doesnt work. I'm new to js syntax if someone could help.

if(document.getElementById("txtArea1").value == '')
            {
                alert("debug");
                document.getElementById("txtArea1").style.display ="none";
                return false;
            };

4 Answers 4

1

I'm not sure if I understand your question correctly but you probably want to compare them with || (OR) operator, so if txtArea1 or txtArea2 is empty then the validation shall not pass. That means both textareas will be required fields.

if (document.getElementById("txtArea1").value == '' || document.getElementById("txtArea2").value == '')
{
    alert("debug");
    document.getElementById("txtArea1").style.display ="none";
    return false;
};
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the "or" is what I needed. Now if i could only know to make the || symbol haha
It should be somewhere above the Enter key on the keyboard. Press it with the shift key. Of course it's a single | character. You have to press it twice.
1

Double && specifies the AND condition.

if (document.getElementById("txtArea1").value == '' && document.getElementById("txtArea2").value == '')

2 Comments

oh ok, thanks. However this seems to validate only txtArea1 and disregards txtArea2.
If txtArea1 fails that condition, then it doesn't care about txtArea2, it's an AND, so if one fails, the entire block is jumped./
0

If you want to treat both separately, you'll have to use two separate if statements as well. (I outsourced the textareas into variables for readability)

var txtarea1 = document.getElementById("txtArea1");
var txtarea2 = document.getElementById("txtArea2");
if(txtarea1.value == '')
{
    alert("debug");
    txtarea1.style.display = "none";
    return false;
};
if(txtarea2.value == '')
{
    alert("debug");
    txtarea2.style.display = "none";
    return false;
};

If you want to do one thing if either of them (1 or 2) is empty, try this:

if(txtarea1.value == '' || txtarea2.value == '')
{
    alert("debug");
    txtarea1.style.display ="none";
    txtarea2.style.display ="none";
    return false;
};

Comments

0
var t1 = document.getElementById("txtArea1").value;
var t2 = document.getElementById("txtArea2").value;
if( t1 == '' || t2 == '')
{
   alert("debug");
   document.getElementById("txtArea1").style.display ="none";
   return false;
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.