1

Hi I need submit a page after some validation is done. I never used jQuery before, any idea how to do it? Script come from this tutorial http://webcloud.se/log/Form-validation-with-jQuery-from-scratch/

<form method="post" action="/contact-process" id="demo-form">
 <script>
 var demoForm = $("#demo-form");
 demoForm.validation();
 demoForm.submit(function (e) {
 $("#valid-form").remove();
 if (demoForm.validate()) {
 demoForm.append("<strong id='valid-form'>Form is valid!</strong>");
 // Here submit the page
 }
 e.preventDefault();
 });
 </script>
asked Sep 7, 2011 at 14:02
3
  • document.name_of_the_form.submit Commented Sep 7, 2011 at 14:05
  • If you want the page to just submit normally and page refresh after validation, just put the e.preventDefault() in an } else { after that if. Commented Sep 7, 2011 at 14:06
  • Is the element with the id valid-form, a <form>? Commented Sep 7, 2011 at 14:08

7 Answers 7

3

Since you're handling the form's submit event, you only need to call preventDefault() if it is invalid:

demoForm.submit(function(e) {
 $("#valid-form").remove();
 if (demoForm.validate()) {
 demoForm.append("<strong id='valid-form'>Form is valid!</strong>");
 } else {
 e.preventDefault();
 }
}
answered Sep 7, 2011 at 14:05
Sign up to request clarification or add additional context in comments.

Comments

2

Change your handler to only prevent default if the form is invalid:

demoForm.submit(function (e) {
 $("#valid-form").remove();
 if (demoForm.validate()) {
 demoForm.append("<strong id='valid-form'>Form is valid!</strong>");
 }
 else
 e.preventDefault();
});
answered Sep 7, 2011 at 14:06

1 Comment

While InvisibleBacon has answered your question correctly, it doesn't address style issues, regarding how and where you use jquery calls. Most importantly, wrapping everything in $(document).ready() so that nothing can fire that depends on a DOM element that hasn't finished loading yet. Also, if all you want to do is stop submission if the form is invalid, then you simply need an if statement that does that when the value is false, no need for if/else at all.
1

Try adjusting your script code to only prevent the submit if validation fails. For example the following code (since you only provided a snippet, I just created a basic page that does essentially what you want, you'll have to adjust your code as needed)

<html>
<head>
<title></title>
 <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js" type="text/javascript"></script> 
 <script type="text/javascript">
 $(document).ready(function() {
 var valid = true;
 $("form").submit(function (e) {
 if (!valid) {
 e.preventDefault();
 }
 });});
 </script>
</head>
<body>
<form method="post" action="anotherpage.htm">
 <input type="text" value="hello"></input>
 <input type="submit" value="submit"></input>
</form>
</body>
  1. Calling .preventDefault (side note: you can also return false) stops the submit from actually taking place. If the form is valid, it skips the if statement and continues with the submit.
  2. You should place your jquery code inside $(document).ready(function() {}); because you don't want anything to fire before the DOM is fully loaded.
  3. Generally, you should place your script tag in the HEAD element
answered Sep 7, 2011 at 14:32

Comments

0

Maybe you can try:

$("#valid-form").submit();

But...in your code your removing the form, so there's nothing to submit. Maybe you can use hide() instead of remove().

answered Sep 7, 2011 at 14:04

4 Comments

I get this error: Line: 42 Error: Object doesn't support this property or method source code for js here any ideas? webcloud.se/log/Form-validation-with-jQuery-from-scratch/…
Well yeah, of course that is because you removed the form first!
strange thing, I receive an error Error: Object doesn't support this property or method but after the page can been submitted erro is in jqery.min.js
If you try to figure out which object the message is about...you should be on the roll again.
0

Try using $("#valid-form").submit();

answered Sep 7, 2011 at 14:04

1 Comment

I tried but i get an error: Object doesn't support this property or method source code for js here any ideas?
0
$("#valid-form").submit(); 
answered Sep 7, 2011 at 14:05

Comments

0

If form is valid just return true else return false it will stop the form to submit.

 demoForm.submit(function (e) {
 $("#valid-form").remove();
 if (demoForm.validate()) {
 demoForm.append("<strong id='valid-form'>Form is valid!</strong>");
 return true; 
 }
 return false;
 });
answered Sep 7, 2011 at 14:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.