0

I understand that this is very stupid, but the code between tags just doesn't work.

I have a simple HTML registration form. And the js-code makes a simple check is any input is empty.

function validate() 
{
    if (document.forms.registration.email.value == "")
    {
        alert("You must provide an email address.");
        return false;
    }
    else if (document.forms.registration.password1.value != document.forms.registration.password2.value)
    {
        alert("You must provide the same password twice");
        return false;
    }
    else if (!document.forms.registration.agreement.checked)
    {
        alert("You must agree to ours terms and conditions");
        return false;
    }
    return true;
}

For some reason the browser kind of ignores this code and submits the form, even if it's empty.

EDIT:(Added based on OP comment)

<form action="process.php" id="registration" method="get" onsubmit="return.validate();">
        <!-- Other child elements of the form -->
</form>
7
  • 3
    Are you actually binding this function to your form's submission event? Commented Aug 29, 2013 at 16:17
  • 2
    Please show how you're using this function. Commented Aug 29, 2013 at 16:19
  • @meagar & Claudio Redi. For both of you. Here is how I am using this js-code: <pre><code><form action="process.php" id="registration" method="get" onsubmit="return.validate();"></code></pre> Commented Aug 29, 2013 at 16:25
  • 2
    You don't need the dot, just return validate();. Check for javascript errors in your debugging console. Commented Aug 29, 2013 at 16:30
  • :) I don't have "pre" and "code" tags in my original code. I wanted to add it here, in comments for stylistic reasons. Commented Aug 29, 2013 at 16:52

1 Answer 1

1

Replace onsubmit="return.validate();" by onsubmit="return validate();".
As @showdev did say :

You don't need the dot.


You could also use a javascript library such as jQuery to produce your code.

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

1 Comment

Thank you so much! It works now! I am studying web development by Harvard's CS-75 course. And this code I wrote is from example. I was really stupid because the right version was written even in the example. So, really thank you. By the way, why did apache give me an error like this? I mean favicon.ico.

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.