问题描述:

I set a maxlength on the textarea field. How do I prevent maxlength from blocking submit in cases where the user goes above the maxlength of the textarea?

网友答案:

You can do that using Javascript / jQuery. Check the length of your text area, and if it is more that what you want(maxt length) give an alert and do not allow to submit the form.

<script>
    function formSubmit(){
        var count = $("#your_textarea").val().length;
        if(count >= MAX_LENGHT) {
            alert("Max length reached for the text area");
            return false;
        }
    }
</script>

<textarea id="your_textarea"></textarea>
<input type="button" onclick="formSubmit()" value="Submit form">

note: codes are not tested.
网友答案:

Hope Below code help you.

<html>
<head>

<script>
function test()
{
    var txtval_length=document.getElementById("txt").value.length;
    if(txtval_length>30)  // here 30 is temporary purpose you can set as per your requirement.
    {
        return false; // do not allow to submit
    }
    else
    {
        return true; //  submit form
    }
}
</script>
</head>
<body>
<form action="test1.html" method="post" onsubmit="return test();">
    <textarea id="txt"></textarea>
    <input type="submit" value="Submit"/>
</form>
</body>
</html>

In above example if the length of textarea character is less then 30 then it allow to submit form otherwise form will not submit. you can change condition as per your requirements.

相关阅读:
Top