1

How do I make the form action="connect.php" stop within my function? I've made it so if something is entered incorrectly, then it returns an error;however, if no error is returned then the action="connect.php" takes place, but I can't figure out how to make the action="connect.php" take place if there is an error. How can I make it so it stops, and does not go to connect.php?

html:

<form action="connect.php" method="get" name="registerForm">
            <h3>Not a member yet? Sign Up!</h3>
            <p>Username:</p><input type="text" name="name" id="registerinput_username">
                <div id="usernamereq2"><p>Field is required.</p><img src="ximg.png" alt="x"></div>
            <p>Email:</p><input type="text" name="email" id="registerinput_email">
                <div id="emailreq"><p>Field is required.</p><img src="ximg.png" alt="x"></div>
            <p>A password will be e-mailed to you.</p>
            <input type="submit" name="submit" value="Register" id="registersubmit">
</form>

javascript and jquery:

$("#registersubmit").click(function(){
        if($("#registerinput_username").val() == "" || $("#registerinput_email").val() == "")
        {
            if($("#registerinput_username").val() == "")
            {
                $("#usernamereq2").css({
                opacity:'1'
                });

            }
            if($("#registerinput_email").val() == "")
            {
                $("#emailreq").css({
                opacity:'1'
                });
            }
        }
        if($("#registerinput_username").val() != "")
        {
            $("#usernamereq2").css({
            opacity:'0'
            });
        }
        if($("#registerinput_email").val() != "")
        {
            $("#emailreq").css({
            opacity:'0'
            });
            checkContinue();
        }
        if($("#registerinput_email").val() != "" && $("#registerinput_username").val() != "")
        {   
            validateFormRegister();
        }
    });

function validateFormRegister()
    {
        var email=document.forms["registerForm"]["email"].value;
        var atpos=email.indexOf("@");
        var dotpos=email.lastIndexOf(".");
        var name=document.forms["registerForm"]["name"].value;
        var x=name.length;
            if(atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
                {
                alert("Email is invalid.");
                }
            if(x<5)
                {
                alert("Username is too short! Must be at least 5 characters.");
                }
    }

4 Answers 4

4

You can use preventDefault() to stop a defult event from occurring:

$("#registersubmit").click(function(e){
    e.preventDefault();

When your validation is complete you can then call .submit() to submit the form.

<!-- add id attribute t form -->
<form action="connect.php" method="get" id="registerForm" name="registerForm">

// add submit() to actually submit form
$("#target").submit();
Sign up to request clarification or add additional context in comments.

Comments

1

Add event.preventDeafult() at the end of the click event:

$("#registersubmit").click(function(event){
    event.preventDeafult();
)};

Comments

0

you could remove the submit type on your button on page load and then toggle it between button and submit based upon error.

var button = $('#registersubmit');
button.prop('type','button');
var error = true;

// perform error checking
if(!error){
    button.prop('type', 'submit');
}

http://jsfiddle.net/j9SN7/

Comments

0

you can use preventDefault to stop submit.

$("#registersubmit").click(function(e){
   {form validation here}
   if(form is invalid ) 
       e.preventDefault();
   }
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.