Jump to content

Make submit clickable when form is filled?


LemonInflux

Recommended Posts

I have a form like this:

 

<form action="test.php">
<input name="message" type="text" />
<input type="submit" name="Submit" value="Submit" />
</form>

 

My question is, is there a way to make the submit button be disabled until the text input box has something in it?

 

I know you can disable submits like this: document.formName.submitName.disabled=true;

And I could work out how to make the submit unclickable after form submit (onSubmit), but I can't think of a simple way of doing this.

 

Thanks in advance,

 

Tom

 

Here you go Tom; try this out and see how it works out for you. :)

 

<script language="javascript">
function valsub()
{
var field = document.getElementById('msg').value;
if (field.length >= 1 && field != null && field != "")
{
document.getElementById('btn').disabled = false;
}
else {
document.getElementById('btn').disabled = true;
}
}
</script>

<form action="test.php">
<input id="msg" name="message" type="text" onkeyup="valsub()" />
<input id="btn" type="submit" name="Submit" value="Submit" disabled />
</form>

 

 

ok  :P

 

One last question, however.

 

When checking for XHTML compliancy, it had an error.

 

<input id="btn" type="submit" name="Submit" value="Submit" disabled />

 

It needs to be disabled="something" apparently. What is the something?

<script language="javascript">

function valsub()

{

var field = document.getElementById('msg').value;

var field2 = document.getElementById('field2').value;

var field3 = document.getElementById('field3').value;

if (field.length >= 1 && field != null && field != "" && field2.length >= 1 && field2 != null && field2 != "" && field3.length >= 1 && field3 != null && field3 != "")

{

document.getElementById('btn').disabled = false;

}

else {

document.getElementById('btn').disabled = true;

}

}

</script>

 

You would then do a little something like this:

 

<script language="javascript">
function valsub()
{
var field = document.getElementById('msg').value;
var field2 = document.getElementById('msg2').value;
var field3 = document.getElementById('msg3').value;
if (field.length == 0 || field == null && field == "")
{
document.getElementById('btn').disabled = true;
}
else if (field2.length == 0 || field2 == null && field2 == "")
{
document.getElementById('btn').disabled = true;
}
else if (field3.length == 0 || field3 == null && field3 == "")
{
document.getElementById('btn').disabled = true;
}
else {
document.getElementById('btn').disabled = false;
}
}
</script>

<form action="test.php">
<input id="msg" name="message" type="text" onkeyup="valsub()" />
<input id="msg2" name="message2" type="text" onkeyup="valsub()" />
<input id="msg3" name="message3" type="text" onkeyup="valsub()" />
<input id="btn" type="submit" name="Submit" value="Submit" disabled />
</form>

 

I mean, there are other ways to go about do that; but using my original example above - you would do something like this.

Hooray for javascript. It's a great language, I'm enjoying learning it. My only problem with it, is I can't find an online manual (as in, full manual, like php.net for php). I seem to have to go trawling through google to find what I want.  :(

OK, final question  :P

 

I'm doing a bit of ajax.

 

I want this form to submit to addComment.php without refreshing

 

I've figured out I'm probably going to pass the comments through the URL, so...

 

What I want to do is have my form so that when submit is clicked, the ajax goes to addComment.php?comment=myComment&user=userName or whatever it needs to go to. So, how do I get the value of input without submitting the form to another page?

 

You see where the function is being triggered; with the onchange() event, in the input text field? All you need to do is remove the onchange event from the input text field and add the "ajaxFunction()" to a button (that has an onclick event).

 

Example:

 

<input type="button" onclick="ajaxFunction()" value="Submit">

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.