0
<?php
if (isset($_POST['submit']) ) {
//send to database
}
?>

<!DOCTYPE html>
<html lang="en">

<head>
    <script>
    function validateForm() {
        var usernameentry = document.forms["registrationform"]["username2"].value;
        var passwordentry = document.forms["registrationform"]["password2"].value;
        var nameentry = document.forms["registrationform"]["password2"].value;

        var emailentry = document.forms["registrationform"]["email"].value;
        var atpos = emailentry.indexOf("@");
        var dotpos = emailentry.lastIndexOf(".");



        if (usernameentry.length < 3  ||  username.length > 20){
            alert("Username must be inbetween 4 and 20 characters");
            return false;
        }

        else if (passwordentry.length < 3  ||  password.length > 20){
            alert("Password must be inbetween 4 and 20 characters");
            return false;
        }

        else if (nameentry.length < 3  ||  name.length > 45){
            alert("Name must be inbetween 4 and 45 characters");
            return false;
        }


        else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=emailentry.length  || emailentry.length > 154) {
            alert("Not a valid e-mail address");
            return false;
        }

        else
        {
            return true;
        }
    }
    </script>
</head>

<body>  
    <form name="registrationform" method="post" action="login.php" onsubmit="return validateForm();">

    Name: <input type="text" name="name"/>
    <br/>
    <br/>
    Email: <input type="text" name="email"/>
    <br/>
    <br/>
    Username: <input type="text" name="username2"/>
    <br/>
    <br/>
    Password: <input type="password" name="password2"/>
    <br/>
    <br/>
    <input type = "submit" name = "submit" value = "submit" />
    <br/>
    <br/>
    </form>
</body>

I want the contents of the if statement to run ONLY when the form has been validated with JavaScript, it runs regardless of whether the value returns is true or false.

I'm guessing what I need to do is similar to

if (isset($_POST['submit']) && onsubmit == true)

Obviously that's not right, but I don't know how to do it.

I know validating with php is a much more logical approach, but I need to demonstrate use of JavaScript.

3
  • What you've written should do exactly what you want. However, keep in mind that you can't rely on JavaScript actually running since it's possible to post to this page from a different location. But for a simple solution, this shoudl do it. Commented Jan 30, 2015 at 4:25
  • no need to add that "onsubmit" inside the PHP if, the onsubmit already ran when without errors on front when it returned true and continued the form submission. Commented Jan 30, 2015 at 4:25
  • The problem is that the contents of the if statement run regardless whether or not the value JavaScript returns is true or false. Commented Jan 30, 2015 at 4:44

1 Answer 1

0

You don't need to do that. When the form is validated, it will be sent to login.php

You can see this question HTML/Javascript: Simple form validation on submit

Also, there are a lot of libraries which could help you http://www.javascript-coder.com/html-form/javascript-form-validation.phtml

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

1 Comment

I figured out it wasn't working properly due to the cache in Google Chrome. Thanks anyway

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.