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

Discussion in 'PHP' started by kriiv, Sep 21, 2012.

  1. #1
    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 ways to do it, with no luck.

    Any tips or help?

    Thanks!
     
    kriiv, Sep 21, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #2
    That has nothing to do with PHP. That is all done via javascript. So next time please post in the correct board.

    
    <textarea style="width: 300px; height: 300xp;" id="text" onkeydown="checkWordCount();"></textarea>
    <input type="submit" name="submit" value="Submit" id="submit" disabled />
    <script type="text/javascript">
    function checkWordCount() {
    	var wordCount = document.getElementById('text').value.split(" ").length;
    	
    	if (wordCount > 10) {
    		document.getElementById('submit').disabled = false;
    	}
    	else {
    		document.getElementById('submit').disabled = true;
    	}
    }
    
    
    </script>
    
    Code (markup):
     
    plussy, Sep 21, 2012 IP
  3. Poppers

    Poppers Member

    Messages:
    61
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    28
    #3
    As above, it is done with Javascript - HOWEVER, I would recommend a PHP backup measure too to check the field for the desired length, as after all the user could have Javascript disabled ;)
     
    Poppers, Sep 21, 2012 IP
  4. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #4
    That is true but in this day and age only a very very small number of people have javascript disabled.
     
    plussy, Sep 21, 2012 IP
  5. gigpayrr

    gigpayrr Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    quick solution would be as plussy explained. good piece of code
     
    gigpayrr, Sep 26, 2012 IP
  6. RaPeRbOy

    RaPeRbOy Active Member

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    90
    #6
    You can only do this using JavaScript. Also, make a php check, because javascript can be disabled.
     
    RaPeRbOy, Oct 3, 2012 IP
  7. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #7
    Use the javascript code which is submitted by "plussy". Then after submitting re-check in php.
     
    HungryMinds, Oct 3, 2012 IP