2

html code

<div id="signup">
    <form  id="suform" method="POST" action="roma/roma">
        <p>
            <label>Frist Name</label>
            <input type="text" id="sufName"/>
            <span class="errorMessage"></span>
        </p>
        <p>
            <label>Last Name</label>
            <input type="text" id="sulName"/>
            <span class="errorMessage"></span>
        </p>
        <p>
            <label>Email</label>
            <input type="text" id="suEmail"/>
            <span class="errorMessage"></span>
        </p>
        <p>
            <label>Mobile Number</label>
            <input type="text" id="suMNumber"/>
            <span class="errorMessage"></span>
        </p>
        <p>
            <label>Password</label>
            <input type="password" id="suPassword"/>
            <span class="errorMessage"></span>
        </p>
        <p>
            <label>Re Password</label>
            <input type="password" id="suRePassword"/>
            <span class="errorMessage"></span>
        </p>
        <p>
            <input type="submit" class="button" value="sign up"/>
        </p>
    </form>
</div>

it is just six input fields to make a sign up page and this is the jQuery code to ensure that there is no input field empty, and if there is no input field empty I want to submit the form.

jQuery code

$(document).ready(function(){
    $('#suform').on('submit', function(e){
        e.preventDefault();
        var errorCount = 0;
        $('span.errorMessage').text(''); // reset all error mesaage
        $('input').each(function(){
            var $this = $(this);
            if($this.val() === ''){
                var error = 'Please fill ' + $this.prev('label').text(); // take the input field from label
                $this.next('span').text(error);
                errorCount = errorCount + 1;   
            }
        });
        if(errorCount === 0){
            $(this).submit(); // submit form if no error
        }
    });
});

The code ensure that there is no input field empty, it found an empty one then an error message will appear, else should submit, but the submit doesn't work.

code

5
  • I don't know the exact problem. but you know there's this function in jquery? api.jquery.com/submit Commented May 12, 2012 at 14:33
  • and i am using it, see it the last line in jquery code , the question is why this submit doesn't work ? Commented May 12, 2012 at 14:33
  • 2
    You should really consider using a jQuery plugin for validation instead of this. Commented May 12, 2012 at 14:35
  • 1
    @WilliamKinaan ah I didn't spot it ^^; Commented May 12, 2012 at 14:37
  • 1
    try this: if(errorCount !== 0){ return false; } and test your form with other browsers too. Commented May 12, 2012 at 14:40

4 Answers 4

3

Try using $(this)[0].submit();. Using $(this) refers to the jQuery object reference to the form. I don't know what jQuery's submit() method actually does, but it clearly doesn't submit the form. Using $(this)[0] refers to the actual DOM element, which will use the standard DOM submit method, which is what you're looking for.

I'm sure you know this already, but you should use server and client side validation, because some users don't have JS and some users will purposely tamper with your form. Just a quick "Don't forget"!

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

1 Comment

you the one man , it works thank you i will accept the answer when i allow to do that
3

Check out the jQuery validation plugin. For this you add a class = "required" for any input field that is required, and just call validation on it:

$('form').validation({
  // any options you want
});

It will also do handy things like displaying error messages, doing conditional validation, etc.

1 Comment

Yep exactly. You'll need to include the actual plugin, and the documentation is all available on the link I provided.
1

You can remove e.preventDefault() and use return insead:

$('form').on('submit', function(e){
    var errorCount = 0;
    $('span.errorMessage').text('');
    $('input').each(function(){
        var $this = $(this);
        if($this.val() === ''){
            var error = 'Please fill ' + $this.prev('label').text();
            $this.next('span').text(error);
            errorCount = errorCount + 1;   
        }
    });
    return errorCount === 0;
});

DEMO: http://jsfiddle.net/h9njC/27/

2 Comments

thank you , but i need to make e.preventDefault , the answer was $(this)[0].submit()
Just out of interest, why you need e.preventDefault here?
0
$('form').on('submit',function(e){
    var errorCount = 0;
    $('span.errorMessage').text('');
    $('input').each(function(){
        var $this = $(this);
        if($this.val() === ''){
            var error = 'Please fill ' + $this.prev('label').text();
            $this.next('span').text(error);
            errorCount = errorCount + 1;   
        }
    });
    if(errorCount === 0){
        alert('done'); // just an alert to notice when everything OK [remove it]
        $this.submit(); // submit the form on success
    } else e.preventDefault(); // halt the submission on default
  });

DEMO

1 Comment

@William Kinaan I think you want something like this. check the code and the demo

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.