Code Monkey home page Code Monkey logo

Comments (2)

PascaleBeier avatar PascaleBeier commented on May 25, 2024

You can look for the existance of any error classes on the form submission event :)

Currently on mobile, but its alike of

document.getElementById('myform').addEventListener('submit', function (e) {

  e.preventDefault();
  if (document.querySelector('.has-error')) {
    alert('Fix form errors first');
  }
});

from bootstrap-validate.

dominicklee avatar dominicklee commented on May 25, 2024

@bar15150 If you are still looking for a solution for this, you can do the following:

  1. Give your form an ID so that you can refer to it in Javascript.
  2. Then write event.preventDefault() so that the form won't be submitted when the user clicks submit.
  3. Write a new function called validateForm() that is supposed to submit the form once it's valid.
  4. Include jQuery in your HTML. You will need it to find "form errors".

Let's put this all together:

Here is an example of your form tag:

<form method="POST" id="formSignup" onsubmit="event.preventDefault(); validateForm();">
</form>

Add this code before your </body> tag:

<script src="https://code.jquery.com/jquery-3.5.1.min.js" crossorigin="anonymous"></script>
<script>
	function validateForm()
	{
		//Find the number of form errors shown
		var numInvalid = $( "#formSignup" ).find( ".invalid-feedback:not(:hidden)" ).length;

		if (numInvalid == 0) {
			//No errors, submit form
			document.getElementById("formSignup").submit();	//All is good. Submit form.
		}
		else {
			//Errors found, do not submit
		}
	}
</script>

I hope this helps anyone who still wants to use this wonderful library, without waiting for the new version.

from bootstrap-validate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.