Welcome to WebmasterWorld Guest from 54.146.246.4

Forum Moderators: open

Message Too Old, No Replies

Looking for JavaScript to enable form submit button after X characters

Validate minimum content

     

TheSkepticGuy

3:28 pm on Apr 10, 2009 (gmt 0)

5+ Year Member



Hi Folks...

Not a JavaScript expert here... but I've been looking for an example script that would validate text-area content by character count, not allowing the form to be submitted until a minimum of X characters had been typed.

Does anyone have any guidance on this one?

Thanks in advance.

coopster

6:47 pm on Apr 10, 2009 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Use the string length property of the element value. Something like ...
var el = document.getElementById('myTextArea'); 
if (el.value.length > 75) {
// good to go
} else {
// I need more information
}

httpwebwitch

1:44 pm on Apr 13, 2009 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



// note that I'm using the $() shortcut here... because I'm lazy

var el = $('myTextArea');
var sb = $('mySubmitButton');

// have the behaviour trigger on keyup for best user responsiveness
// the length condition returns true or false.
// assign that to the "disabled" property of your submit button

el.onkeyup = function(){
sb.disabled = (this.value.length < 75);
}

tada!