1

I'm using a button element to submit values. I want to check, if the input fields are filled

The 'required'-attribute does not work in this specific case, because I'm using pickadate.js, which disables that.

I made a javascript-function that is called on a button click. To stop the button from submitting, I've set the return to false:

<button type="submit" class="btn btn-primary"
onclick="checkIfFilled(); return false;" 
data-loading-text="loading…">ButtonText</button>

Now I want to set the return false; to return true; using the javascript function checkIfFilled()

So if the check was successful, the data should be send like if there was no return false;

Edit for those who may have the question in the future: The solution is to return the function, which returns either true or false whenever you want it.

Thanks to Michael, musically_ut and Amani!

Thanks for reading!

3
  • 3
    Why not onclick="return checkIfFilled()" ? Commented Aug 20, 2015 at 9:27
  • Remove the return false; from the HTML and return true/false from checkIfFilled itself. Commented Aug 20, 2015 at 9:27
  • I'm not used to javascript, but this solution is great! Thanks to you! Commented Aug 20, 2015 at 9:30

2 Answers 2

1

you can do something like this:

<form method="post" action="#">
<input id="check_this" name="" value=""></input>
<button type="submit" class="btn btn-primary"
onclick="return checkIfFilled()" 
data-loading-text="loading…">ButtonText</button>
</form>

<script type="text/javascript">
    function checkIfFilled(){
      //if required input is empty retuen false if not return true
      if(document.getElementById("check_this").value != "") {
        return true;
      } else {
        return false;
      }
    }
</script>

Sign up to request clarification or add additional context in comments.

1 Comment

Even if I already got it, thats a good example for the future. Thanks! :)
1
<button type="submit" class="btn btn-primary" onclick="return checkIfFilled();" 
data-loading-text="loading…">ButtonText</button>


<script type="text/javascript">
    function checkIfFilled(){
      if(document.getElementById("textboxId").value.trim()!=="") {
         return true;
      }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.