1

I have made a add button in upper right corner (see 1st screenshot) when I click on it, it displays form which has 3 input fields for adding name, contact number, group name and at last submit button.

I have added required attribute in input field of form but it does not work when I click on submit button it submits blank details (see 2nd screenshot)

in app.js:

I am dynamically adding HTML using javascript:

    <div class="row">
    <div class="col-lg-12" id="addNewContact">
     <form>
      <div class="form-group">
          <label for="name">Name:</label>
          <input type="text" class="form-control" id="nameInput">
      </div>
      <div class="form-group">
          <label for="number">Number:</label>
          <input type="text" class="form-control" id="numberInput">
      </div> 
      <div class="form-group">
          <label for="group">Group:</label>
          <input type="text" class="form-control" id="groupInput">
      </div> 
      <button type="button" class="btn btn-success" onclick="submitToDb()">Submit</button>
    </form>
    </div>
</div>

Screnshot:

enter image description here

Blank fields are displayed see screenshot:

enter image description here

8
  • change your type to button and not submit, you bind an onclick to an element that submits the entire form and does not trigger your javascript. Commented Feb 8, 2018 at 16:44
  • @Programmer I am trying this example w3schools.com/js/tryit.asp?filename=tryjs_validation_html Commented Feb 8, 2018 at 16:46
  • @Programmer If the browser doesn't know it's submitting, why will it perform the required checks? Commented Feb 8, 2018 at 16:46
  • @Barmar I am trying this example w3schools.com/js/tryit.asp?filename=tryjs_validation_html Commented Feb 8, 2018 at 16:46
  • Are you certain that your input values are empty? What happens if you remove the value attributes in your html? Commented Feb 8, 2018 at 16:47

1 Answer 1

3

Instead of using onclick, use onsubmit on the form.

<form onsubmit="submitToDb()">

Form validation is done when processing the submit event. If you use a button click to submit the form, that happens before the default submission occurs.

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

13 Comments

As far as I can tell, the only difference is the name of the function.
I changed onclick() to onsubmit() and now it works like a charm. If you liked my question please do upvote it. Thank you :)
I onsubmit() is not working ? when I click it on submit it does not submits it onclick it just becomes static why so ?
You don't have required in the jsfiddle.
when I click on submit button with onclick() it atleast displays blank fields but with onsubmit() it does nothing ?
|

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.