2

I have a page which has 2 text areas and wanted to validate each text area if the characters being typed is more than max length upon clicking submit button. Problem with the code below when clicking submit it can only detect if textarea1 is more than max length but not the other textarea. What I wanted to do is it can determine which textarea is exceeding maxlength so users can correct it. Thanks in advance.

 <html>
<head>

<script>
function testing()
{
    var txtlength=document.getElementById("txt1").value.length;
    if(txtlength>50)  
    {
        alert("Max1 length reached ");
        return false; 
    }
    else
    {
        return true; 
    }

    var txtlength1=document.getElementById("txt2").value.length;
    if(txtlength1>50)  
    {
        alert("Max2 length reached ");
        return false;
    }
    else
    {
        return true;
    }

}
</script>
</head>
<body>
<form action="test1.html" method="post" onsubmit="return testing();">
    <textarea id="txt1"></textarea>
    <textarea id="txt2"></textarea>
    <input type="submit" value="Submit"/>
</form>
</body>
</html>
1
  • 2
    return exits the function. The second block of code is never reached. Commented Feb 20, 2014 at 3:39

5 Answers 5

2

the return true statement should only be done once, at the end of the code.

the final code should looks something like this :

<script>
function testing()
{
    var txtlength=document.getElementById("txt1").value.length;
    if(txtlength>50)  
    {
        alert("Max1 length reached ");
        return false; 
    }

    var txtlength1=document.getElementById("txt2").value.length;
    if(txtlength1>50)  
    {
        alert("Max2 length reached ");
        return false;
    }

    return true;
}
</script>           
Sign up to request clarification or add additional context in comments.

Comments

2
function testing()
{
    var txtlength=document.getElementById("txt1").value.length;    
    var txtlength1=document.getElementById("txt2").value.length;
    if(txtlength>50)  
    {
        alert("Max1 length reached ");
        return false; 
    }
    else if(txtlength1>50)  
    {
        alert("Max2 length reached ");
        return false;
    }
        return true;  
}

1 Comment

Thanks Raja but i have to give the points to andri since he answered first.
1

Change your following function:

var txtlength=document.getElementById("txt1").value.length;
    if(txtlength>50)  
    {
        alert("Max1 length reached ");
        return false; 
    }
    else
    {
        return true; 
    }

    var txtlength1=document.getElementById("txt2").value.length;
    if(txtlength1>50)  
    {
        alert("Max2 length reached ");
        return false;
    }
    else
    {
        return true;
    }

to:

var txtlength=document.getElementById("txt1").value.length;
        if(txtlength>50)  
        {
            alert("Max1 length reached ");
            return false; 
        }
        else
        {
            var txtlength1=document.getElementById("txt2").value.length;
            if(txtlength1>50)  
            {
            alert("Max2 length reached ");
            return false;
            }
            else
            {
            return true;
            }
        }

2 Comments

How about an else if statement?
Thanks BelgianMyWaffle
1

Rather than hard–coding the form ID, pass a reference to the from from the listener:

<form ... onsubmit="return testing(this);">

In the function, loop over the form's controls and check them:

function testing(form) {
  var control;

  for (var i=0, iLen=form.elements.length; i<iLen; i++) {
    control = form.elements[i];

    if (control.tagName.toLowerCase() == 'textarea' && control.value.length > 50) {
       alert(control.name + ' must be 50 characters or less');
       return false;
    }
  }
}

}

1 Comment

Thanks Robg this is an interesting solution :)
0

Another way to do is the same as Raja Dani post, to place a new var called errormessage as empty string above txtlength,txtlength1, replace the alert with adding those messages in errormessage var, output alert if the errormessage length > 0 and false else true? :)

That is just not like the best solution but another way to solve it.

I am new here, tried to add posts on edit (you guys make it so complex) to only add a comment and not edit it cause I cannot comment other posts as I have low rep.

1 Comment

in time you'll gain more points so. They both have the same answer with andri

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.