Stop submit button being pressed until enough words are written in text area

后端 未结 6 2085
感动是毒
感动是毒 2021-01-25 17:24

So basically what I want is for the submit button to be disabled until a certain word count has been reached in the text area.

I have had a look around and tried to find

相关标签:
6条回答
  • 2021-01-25 17:48

    You can check the word/charactor count in keyup event.

    0 讨论(0)
  • 2021-01-25 17:52

    Try out below lines of code,

    $(document).ready(function() {
        $(".submit-name").attr("disabled", "true");
        var minLength = 100;
        $("#your-text-area").bind('keyup', function(event) {
            var String = $("#your-text-area").val()
    
               if (String.length >= minLength )  {  
                    $(".submit-name").removeAttr("disabled");
                } else {
                    $(".submit-name").attr("disabled", "true");        
                }
    
    });
    
    
    
    });
    
    0 讨论(0)
  • 2021-01-25 17:57

    Show us your code.
    1) Set onclick event for the submit button
    2) Check length in textarea
    3) return false and preventDefault() if there is not enough words.

    0 讨论(0)
  • 2021-01-25 18:00

    You catch the submit event, you count the words in the textarea and only submit if it is high enough. Example:

    $('#targetForm').submit(function(event) {
        var text = $("#myTextarea").val();
        text = text.split(" ");
        // check for at least 10 words
        if(text.length < 10){
            // prevent submit
            event.preventDefault();
            return false;
        }
    });
    

    DEMO

    0 讨论(0)
  • 2021-01-25 18:06

    see this example at http://elylucas.net/post/Enabling-a-submit-button-when-a-textbox-has-value-in-jQuery.aspx

    instead of checking for a value, you split the input string in the text box and see the length of the array is more than your desired word count

    0 讨论(0)
  • 2021-01-25 18:15

    Use the textchange (not built-in) event to accurately detect text changes via keyboard, paste etc. - http://www.zurb.com/playground/jquery-text-change-custom-event

    $('textarea').bind('textchange', function () {
       // $(this).val() contains the new text
    });
    

    In your text-change event, check the length/count the words in your text and enable disable the submit button as needed (make sure its initially disabled).

    0 讨论(0)
提交回复
热议问题