-1

I'm trying to do some extremely simple form validation, my current problem is that my window.onload function doesn't call in the function I specify.

When I watch the flow of logic with firebug it just skips to the end of the code.

Here is an example of my code:

window.onload = init;

function init() {
    var regForm = document.getElementById("registerform");
    regForm.onsubmit = validatepostcode();
}

function validatepostcode() {
    var postCode = document.getElementById("postcode");
    var postCodeStr = postCode.charAt(0);
    var state = document.getElementById("state");
    var result = true;

    if (postCodeStr == 3 || 8 && state == "Vic") {
        result = true;
    } else if (postCodeStr == (1 || 2) && state == "NSW") {
        result = true;
    } else if (postCodeStr == (4 || 9) && state == "QLD") {
        result = true;
    } else if (postCodeStr == 0 && state == "NT" || state == "ACT") {
        result = true;
    } else if (postCodeStr == 6 && state == "WA") {
        result = true;
    } else if (postCodeStr == 5 && state == "SA") {
        result = true;
    } else if (postCodeStr == 7 && state == "TAS") {
        result = true;
    } else
        result = false;

    if (result = false) {
        alert("Your postcode does not match your state")
    }
}

1 Answer 1

2

Five problems:

  1. In init, you have this:

    regForm.onsubmit = validatepostcode();
    

    That calls validatepostcode and puts its return value in onsubmit. You probably meant to put the function itself it, not its return value in. Remove the parentheses:

    regForm.onsubmit = validatepostcode;
    
  2. In validatepostcode, you're fetching elements like this:

    var postCode = document.getElementById("postcode");
    

    …but then try to use them as values, like this:

    var postCodeStr = postCode.charAt(0);
    

    But an element and the current value of that element are not the same thing. More likely, you meant to retrieve the value on the first line:

    var postCode = document.getElementById("postcode").value;
    

    Same goes for state.

  3. In validatepostcode, you have lines like this:

    } else if (postCodeStr == (1 || 2) && state == "NSW") {
    

    Specifically, 1 || 2 won't work like that. It will look at them like booleans and say, “one or two? well, they're both truthy…true it is!” and you'll essentially be doing

    } else if (postCodeStr == true && state == "NSW") {
    

    (Actually, it uses 1, not true, since the first operand was truthy, but that's not the important point here.)
    Instead of using that abbreviated notation, you'll have to write it out longhand:

    } else if ((postCodeStr == 1 || postCodeStr == 2) && state == "NSW") {
    
  4. You mixed up = and == here:

    if(result=false){
    

    = will set result to false and leave the condition always false. Change it to == to test equality:

    if(result==false){
    
  5. You probably meant to return result at the end to prevent the form from being submitted when there is a validation error. With the other changes applied, you'd get an alert if there was a validation error, but it'd go on submitting anyway. As such, add a return result at the end of the validatepostcode function.

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

2 Comments

Hey, thanks a lot! I have fixed up all of those errors, unfortunately it still doesn't seem to want to validate. It will proceed with Init, but not call in postcodevalidate on the regForm submit.
It's now working perfectly, thanks a lot. I clearly have a lot to learn about this language especially with no C-family experience. I hope you have a brilliant day!

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.