12

First I want to validate some of the field values entered or not. After that When i click the submit button, the button must be disabled after one click to avoid duplicate submission. How to do that using javascript?

<script type="text/javascript" language="javascript">
    function ValidateIt() {
      if (document.getElementById('ddlProblemCategory').value == 0) {
           alert("Please fill some value");
           return false;
      }
           return true;
    }
    function DisableIt() {
        if (ValidateIt() == true)
          document.getElementById('btnSaveProblem').disabled = true;
    }  
</script>

3
  • If you are using jquery, google jquery validate plugin(If you don't use jquery, google javascript validation). Learn you some client side validation. Then If you face any problem with that, come here post the code & ask for help. Commented Jun 14, 2013 at 18:17
  • The following post may be helpful: stackoverflow.com/questions/8165018/… Commented Dec 20, 2013 at 14:43
  • 2
    Upvoted this as it is the first non-jquery result in my google Commented Aug 14, 2014 at 9:12

3 Answers 3

19

You can add an onclick handler to your button:

document.getElementById("idOfButton").onclick = function() {
    //disable
    this.disabled = true;

    //do some validation stuff
}
Sign up to request clarification or add additional context in comments.

1 Comment

In my particular case I was able to just put the onclick right into the input <input type="button" id="btnFinish" class="btn btn-sm btn-primary" value="Finish" onclick="this.disabled = true" />
5

Call function submitbtn onclick of the button.

Use

function submitbtn() {
    getElementById("Submit_id").disabled=true;
   // Validation code goes here
}

3 Comments

You have missed 'd' in disabled...It should be getElementById("Submit_id").disabled=true;
@VigneshVino thanks buddy.. changed as per u..
It should be document.getElementById("Submit_id").disabled=true; You missed the DOM object.
0

Use hidden label and change its value on 1st click

<script type = "text/javascript" language = "javascript">
    function disableButton() {
        var lblText = document.getElementById('lbl_hdn_text').innerHTML;
        if (lblText == "true") {
            document.getElementById('lbl_hdn_text').innerHTML = "false";
            return true;
        }
        else {
            return false;
        }
    }
</script>

<label id="lbl_hdn_text"  style = "display:none;" >true</label>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.