views:

90

answers:

4

Hey Guys,

The validation code that i have on my page is:

function validateForm() {
    with (document.contactform) {
        var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
        if (name.value == "") alertMsg += "\nName";
        if (email.value == "") alertMsg += "\nEmail";
        if (message.value == "") alertMsg += "\nMessage";
        if (alertMsg != "The following REQUIRED fields\nhave been left empty:\n") {
            alert(alertMsg);
            return false;
        } else {
            return true;
        }
    }
}

Also i have added the onsubmit="return validateForm()" into my form section and nothing seems to be working. you can view the source page at http://obliqueinteractive.com/contact.html

Any help will be greatly appreciated Thanks

A: 

one suggestion is try to use document.getElementById("contactform") instead of just document.contactform

I have never used the with command so I am not exactly sure how it is used

{EDIT}

After playing around on your site I believe what I have suggested will fix your problem, let me know how it goes

Ori Cohen
A: 

Add

name="contactform"

attribute to the form tag

or use

document.getElementById("contactform")

instead of

document.contactform
János Harsányi
+3  A: 

Change

<form id="contactform" onsubmit="return validateForm()" action="" method="post">

to

<form name="contactform" id="contactform" onsubmit="return validateForm()" action="" method="post">

The problem occurs when you try to access document.contactform. It's looking for a form named contactform, not a form with an id of contactform.

Rob
I'd keep the `id="contactform"` -- although adding the `name` attribute fixes the problem, removing the `id` may affect the CSS styling of the form.
Donut
A smart way to solve the problem. Without the need to change js file.
Cleiton
Good idea, dgritsko, I'll edit that in.
Rob
A: 

The problem is with the validateForm function.

change:

function validateForm() {
    with (document.contactform) {

to:

function validateForm() {
    with (document.getElementById('contactform')) {
Cleiton