0

i am trying to do a form validation using javascript. if error was found the javascript should stop sending the form to the server.

i made sure that the javascript returns false by writing the statement : return false;

and still the form is sent to the server .

script tag:

<script language="javascript" src="validations.js" type="text/javascript"></script>

my form tag:

FORM id="frm" name="newCustomer" METHOD="POST" ACTION="register.php" onsubmit="return validateNewCustomer()">

JavaScript function :

function validateNewCustomer() {
    var name = document.getElementById('f_name').value;

    var okCustomer = true;

    if (name.value == "") {
        document.getElementById('f_name').value="kossetak";
        document.getElementById('errfname').innerHTML = "Error Name";
        okCustomer = false;
    } else {
        document.getElementById('errname').innerHTML = "";
        okCustomer = true;
    }

    return okCustomer;
}

I appreciate your help. Thank you.

11
  • 3
    Can you post the Javascript function validateNewCustomer()? Commented May 2, 2012 at 15:54
  • Where are you returning false? Show all relevant code, please. Commented May 2, 2012 at 15:54
  • 1
    is there any error in firebug ? Maybe your javascript has errors, and false is not returned, so the form submit Commented May 2, 2012 at 15:55
  • 1
    i've tried using an empty function with the statement return false , and it stops...it seems the problem is in the Javascript code Commented May 2, 2012 at 16:00
  • 1
    Validation should never use true by default. it should always be false unless all checks have been made and your errors is still empty. Commented May 2, 2012 at 16:04

2 Answers 2

8

You always return true from your function, because name.value == "" is always false, so you enter the else clause.

Notice that you already have .value on this line:

var name = document.getElementById('f_name').value

So when you say name.value you are accessing document.getElementById('f_name').value.value which will be undefined.

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

Comments

0

Perhaps you should consider submitting the form inside your JavaScript function rather than using the HTML action parameter.

An example of this would be:

<form id="frm" name="newCustomer" onsubmit="return validateNewCustomer()">...</form>

JavaScript:

function validateNewCustomer() {
    var name = document.getElementById('f_name').value;

    var okCustomer = true;

    // name.value replaced to name
    if (name == "") {
        document.getElementById('f_name').value="kossetak";
        document.getElementById('errfname').innerHTML = "Error Name";
        okCustomer = false;
    } else {
        document.getElementById('errname').innerHTML = "";
        // Submit the document here
        document.forms["newCustomer"].submit();
        // Still supply a return value in case you need it later
        okCustomer = true;
    }

    return okCustomer;
}

Comments

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.