最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Validation for multiple textarea in javascript - Stack Overflow

programmeradmin1浏览0评论

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>

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>
Share Improve this question asked Feb 20, 2014 at 3:35 dimasdimas 2,5976 gold badges42 silver badges67 bronze badges 1
  • 2 return exits the function. The second block of code is never reached. – elclanrs Commented Feb 20, 2014 at 3:39
Add a ment  | 

5 Answers 5

Reset to default 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>           
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;  
}

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

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

}

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 plex) to only add a ment and not edit it cause I cannot ment other posts as I have low rep.

发布评论

评论列表(0)

  1. 暂无评论