0

Hello. i was wondering if this can be done,personally my knowledge of Java / ajax is bad..

How can i make the submit button unable to be clicked if there is no data in the input (usermsg)?

<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="100" />
<input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
</form>

If you have any idea, please help me.
Cheers,
Sorcher

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by twiss
0

hi,
Please try this code.

<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="100" onkeyup="EnableButton();"/>
<input name="submitmsg" type="submit"  id="submitmsg" value="Send" disabled="true"/>
</form>

<script>
function EnableButton(){
	var submitObj = document.getElementById('submitmsg');
	submitObj.disabled = false;
	if(document.getElementById('usermsg').value=="")	{
		submitObj.disabled = true; }
}
</script>
0

If you want to want the button to remain disabled if you enter a space (or 2), add .replace(/^\s+|\s+$/g,'') after .value .

0
<form name="message" action="">
<input name="usermsg" type="text" id="usermsg" size="100" onkeyup="EnableButton();"/>
<input name="submitmsg" type="submit"  id="submitmsg" value="Send" disabled="true"/>
</form>
 
<script>
function EnableButton(){
	var submitObj = document.getElementById('submitmsg');
	submitObj.disabled = false;
	if(document.getElementById('usermsg').value=="")	{
		submitObj.disabled = true; }
}
</script>

Works one time,but after the user has used it once,the code is not enabled anymore

0

By the way, you can somewhat simplify the script to just:

<form name="message" action="">
	<input name="usermsg" type="text" id="usermsg" size="100" />
	<input name="submitmsg" type="submit"  id="submitmsg" value="Send" disabled="true" />
</form>
 
<script>
var submit = document.getElementById('submitmsg');
document.getElementById('usermsg').onkeyup = function() {
	submit.disabled = this.value == '';
};
</script>

Edited by twiss: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.