Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable submit button if field is one field is empty 1

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
Hi Team,

I found some code on this forum that disables the submit button if fields are empty, as I only wanted this to apply to one particular field i hhace tried to edit the code however it does not seem to work. I have posted the code below

Code:
<script language = "javascript">
function maybeDisableButton()
{
 var Pall = document.getElementById("txtPall").value; // Check for value in Pallet Field
 var disableButton = false;
  if (Pall == "") {
   disableButton = true;
  }//end if
  else
  {
   disableButton = false;
  }//end else
 document.frmEnterRecord.Submit.disabled = disableButton;
}//end maybeDisableButton() 
</script>

on the submit button i have the following
Code:
<input name="Submit" type="Submit" id="Submit" value="Submit Check" disabled/>

and on the input field i have
Code:
onKeyUp="maybeDisableButton()"

Could some one tell me what is wrong please


Regards

Paul

 
Another thing to note: Even though JS is case-sensitive, I'd advise renaming your submit button's NAME and ID from 'Submit' to something else, as this could cause confusion with the form's 'submit' method.

Also, I'd change this:

Code:
document.frmEnterRecord.Submit.disabled = disableButton;

to this:

Code:
document.forms['frmEnterRecord'].elements['Submit'].disabled = disableButton;

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Dan,

Thank You for your swift reply, I am sorry I cannot give you a URL as this is on a my company intranet I have made the changes you suggested

Code:
<script language = "javascript">
function maybeDisableButton()
{
 var Pall = document.getElementById("txtPall").value; // Check for value in Pallet Field
 var disableButton = false;
  if (Pall == "") {
   disableButton = true;
  }//end if
  else
  {
   disableButton = false;
  }//end else
 document.forms['frmEnterRecord'].elements['SubmitCheck'].disabled = disableButton;
}//end maybeDisableButton() 
</script>

This is the full detail for the txtbox
Code:
<input name="txtPall" type="text" id="txtPall" size="5" maxlength="3" onKeyUp="maybeDisableButton()"/>

This is now the detail for the submit button
Code:
<input name="SubmitCheck" type="Submit" id="SubmitCheck" value="Submit Check" disabled/>

Still no joy, the button is disabled on load but even when I enter something into the field "txtPall" it still stays disabled

Regards

Paul

 
If I wrap your exact code in a simple html/head/body/form wrapper it works perfectly in both IE6 and Fx2, so I can only assume either:

1. You have other stuff on the page interfering with your script, or

2. Your browsers are broken.

You can try my (also working, slightly cut down) test harness in your browsers to rule out option 2:

Code:
<html>
<head>
	<script language = "javascript">
		function maybeDisableButton() {
			var Pall = document.getElementById("txtPall").value;
			var disableButton = false;
			if (Pall == "") disableButton = true;
			document.forms['frmEnterRecord'].elements['SubmitCheck'].disabled = disableButton;
		}
	</script>
</head>

<body>
	<form name="frmEnterRecord">
		<input name="txtPall" type="text" id="txtPall" size="5" maxlength="3" onKeyUp="maybeDisableButton()"/>
		<input name="SubmitCheck" type="Submit" id="SubmitCheck" value="Submit Check" disabled/>
	</form>
</body>
</html>

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Hi Dan,

This is strange, I cleared my browser cache but it still did not work so i did as you suggested and copied your test harness code to a new page uploaded it and it worked perfectly (Of Course)[bigsmile].

So then I copied your javascript code into my original page, uploaded it and this then also worked perfectly.

Thank you so much for your help.[thumbsup].

You deserve a "star" for this so please accept one.

With regards to your question about a URL ?, does this help you "guru's" when trying to help out people like me with there problems?

Regards

Paul

 
How odd... but at least it works now ;-)

A URL can often help - especially when suspecting interference from other elements on a web page. In this case, it wasn't needed, but can often help, and saves pasting large chunks of code.



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
OK, That makes sense I will see what I can do to make my code accessible in future.

Thanks for the Info

Regards

Paul

 
Dan,

I am english however when I started using tek-tips I was working in the UK for an NZ company called "Kiwiplan" and "kiwieur" was one of the logins we used

Regards

Paul

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top