0

I have a simple html signup form with onsubmit = "return validateForm()" where validateForm() is javascript to ensure input is in the correct form and e-mail and password fields match their confirm fields.

The forms action is set to add.php. add.php adds the input into a mysql database. Both the javascript and php work fine, however even when javascript alerts an error such as "password too short." the php script is run anyways and a new entry made in the database.

I would like the php script to only run upon successful javascript validation, and the user to remain on the same page if any validation fails, how is this done?

I can add code if needed but I think the answer here would be generic.

EDIT:

for example here is the code for validating name fields:

function checkName()
{
    var fName = document.getElementById("firstName");
    var lName = document.getElementById("lastName");
    if (fName.value.length >= 2 && fName.value.match(/^[A-Za-z]+$/) && lName.value.length >= 2 && lName.value.match(/^[A-Za-z]+$/))
        {
            return true;
        }
    else
        {
            return false;
        }
}

function validateForm(){
if (checkName() == false)
        {
            alert("First and Last Name must be at least 2 characters long and contain  only alphabetic characters");
        document.getElementById("fName").focus();
        document.getElementById("fName").select();
        return false;
    }

}

3
  • have validateForm() return false on error. Also, you should validate that stuff in php too before doing an insert because it is crazy easy to bypass javascript validating. Commented Dec 3, 2014 at 0:57
  • This is just a school assignment, security is of no concern. Also my validateForm() does return false upon fail. I will edit the original post with an example function. Commented Dec 3, 2014 at 1:00
  • if you call your function using onclick="return validateForm()" it should stop the form from submitting as jKuhn pointed out Commented Dec 3, 2014 at 1:18

1 Answer 1

1

Is it firstName or fName? If the fName ID doesn't exist, the function will hit an error and exit without returning anything, causing the alert to show, but the form to still submit.

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

4 Comments

the id is firstName I just called the variable fName. That shouldn't matter should it? I see that my focus() and select() are using the wrong id but I fixed those to no success.
The argument of getElementById must exactly match the ID of the element, not a variable.
yes, and the Id for the elements are firstName and lastName. I did however, just change every instance including var names to firstName and lastName and it seemed to work. I don't understand why though I could have called the var poopmonkey if I wanted right?
What happens if you run those two lines in the browser console? Something is causing an error between the alert and the return.

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.