2

I added a javascript form validation to my code, before submitting it to the server. When the user clicks submit it should call the javascript validator before submitting it. If the validator gives the ok, only then should it submit. I seem to be doing something wrong, because it submits regardless of what the validator says...

Html:

<form id="start_form" method="post" onsubmit="return validate_form(this);">
    <ul class="error">
    </ul>
    <label for="title" >Title: </label><input value="{{ post.title }}" type="text" name="title">
    <label for="summery" >Summery/Prompt: <textarea name="summery">{{ post.summery }}</textarea>
    <label for="person">Participants Emails: </label>
    <ul id="people">
        <li><input type="text" name="person1"></li>
        <li><input type="text" name="person2"></li>
    </ul>
    <a href="javascript:void(0)" onclick="add_group();"><img class="img_button" width="30px" height="30px" src="{{ STATIC_URL }}Image/plus_button.png" /></a>
    <a href="javascript:void(0)" onclick="remove_group();"><img class="img_button" width="30px" height="30px" src="{{ STATIC_URL }}Image/minus_button.png" /></a>
    <button type="submit" class="button">Start Game</button>
    <input type="hidden"  id="pn" name="pn" value="2">
</form>

Javascript function:

function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
function append_error(m) {
    c = "<li>" + m + "</li>"
    $('#error').appendChild(c)
    }
function validate_form(form) {
        var e = true
        if (form.title.value == "") {
            append_error("Title Required");
            e = false
        }
        if (form.summery.value == "") {
            append_error("Summery Required");
            e = false
        }
        for (i = 0; i < Number(form.pn.value); i++){
            email = $('[name="person"' + i++ + '"]').innerText
            if(email == ""){
                append_error("Email #" + ++i + " can not be blank")
                e = false
            } else if (!validateEmail(email)) {
                append_error("Email #" + i++ + " is not valid")
                e = false
            }
        }
        return e;
    }
4
  • Check your console for errors - your validate method may be crashing before e can be returned Commented Dec 25, 2012 at 19:35
  • I would, but the page reloads before anything can load to the console.. Commented Dec 25, 2012 at 19:40
  • 1- you do not have ; at the end of most of your javascript lines 2- do not use e as a variable cause it's used for events, change the name of your e variable. Commented Dec 25, 2012 at 19:42
  • Add console.log(m); to append_error(). Set breakpoints in validate_form(). Commented Dec 25, 2012 at 19:47

2 Answers 2

2

jquery doesn't have appendChild method. in your function:

function append_error(m) {
    c = "<li>" + m + "</li>"
    $('#error').appendChild(c)
}

this cause an error that submition eventually ignores your validation function

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

1 Comment

You are correct. The .appendChild() method is a native JavaScript function and not part of jQuery. Reference
0

It looks like you are using jQuery so you can bind the submit event like so:

$('#start_form').submit(function(){
    return validate_form(this);
});

2 Comments

your code wouldn't work.. preventDefauld wont execute the submit, and you need to return true or false $('#start_form').submit(function(e){ return validate_form(this); });
@salexch - This is true. I have edited the example to work for them. In truth I was expecting them to manually send the form submission.

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.