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

Link to comment
Share on other sites

 

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>

 

 

Link to comment
Share on other sites

<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>

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

 

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">

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.