user1
user1

Reputation: 357

How to use javascript validating two text areas

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;
            };

Upvotes: 0

Views: 43

Answers (4)

Adnan Ahmed
Adnan Ahmed

Reputation: 686

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;
};

Upvotes: 0

MildlySerious
MildlySerious

Reputation: 9180

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;
};

Upvotes: 0

matewka
matewka

Reputation: 10158

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;
};

Upvotes: 1

tymeJV
tymeJV

Reputation: 104785

Double && specifies the AND condition.

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

Upvotes: 1

Related Questions