630 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United Kingdom
RobbyDesigns says

I have two simple forms on a web page I’m making – both HTML (not PHP ).

The first form is a simple javascript password gate that sends the user to the next page if their password is correct. The second form is a contact form.

Both forms have their own ‘ID’.

I’m using a simple validation javascript to make sure the contact form has been filled in:

function setFocus(aField) { document.forms0.focus(); }

function isAnEmailAddress(aTextField) {

if (document.forms0.value.length<5) { return false; } else if (document.forms0.value.indexOf(”@”) < 1) { return false; } else if (document.forms0.value.length - document.forms0.value.indexOf(”@”) < 4) { return false; } else { return true; } }

function isEmpty(aTextField) { if ((document.forms0.value.length==0) || (document.forms0.value==null)) { return true; } else { return false; } }

function validate() {

if (isEmpty(“name”)) { alert(“Please fill in your Name.”); setFocus(“name”); return false; }

if (isEmpty(“email”)) { alert(“Please fill in your Email address.”); setFocus(“email”); return false; }

if (isEmpty(“comment”)) { alert(“Please fill in your message.”); setFocus(“comment”); return false; }

if (!isAnEmailAddress(“email”)) { alert(“The entered email address is invalid.”); setFocus(“email”); return false; }

return true;

}

The problem is this – the two forms + vlaidation script simply wont work together; I have to either delete the first form (the password gate) or go without the contact form.

Any ideas?

2957 posts
  • Australia
  • Community Moderator
  • Elite Author
  • Author had a Free File of the Month
  • Most Wanted Bounty Winner
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Contributed a Blog Post
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+10 more
dtbaker Volunteer moderator says
Any ideas?

Post a link to the problem page, the code here is all screwey.

630 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 100 and 1 000 dollars
  • United Kingdom
RobbyDesigns says
Any ideas?
Post a link to the problem page, the code here is all screwey.

Thanks for your time – I’ve upped it to: http://www.robbydesigns.com/temp

You’ll see that the simple ‘login’ form reads a javascript to see if the correct answer has been given, if not it takes the viewer to ‘incorrect.html’.

The trouble is, the main contact form has it’s own javascript that checks the fields have been filled in; that wont work as long as the login form (see above) is on the page – if I delete the login form it works fine. IE I cant have 2 forms on 1 page.

426 posts
  • Beta Tester
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 10 and 49 users
  • Sold between 5 000 and 10 000 dollars
  • United States
vasilios says

I think it’s because you’re writing document.forms[‘0’]. That’s only targeting the first form on the page. For the first form you would write document.form[‘0’] and for the second one it would be document.forms[‘1’]. The form indexes start at zero because JavaScript’s arrays are zero based. :)

...unless I’m not understanding you.

2957 posts
  • Australia
  • Community Moderator
  • Elite Author
  • Author had a Free File of the Month
  • Most Wanted Bounty Winner
  • Author had a File in an Envato Bundle
  • Has been a member for 5-6 years
  • Contributed a Blog Post
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+10 more
dtbaker Volunteer moderator says

Yup try the [1] thing, if that doesn’t work try getElementById instead:


function isAnEmailAddress(aTextField) {

if(typeof document.getElementById(aTextField) != "undefined"){
    if(document.getElementById(aTextField).value.length<5){
        return false;
    }else if(document.getElementById(aTextField).value.indexOf("@ ") < 1){
        return false;
    }else if(document.getElementById(aTextField).value.length - document.forms[0][aTextField].value.indexOf("@ ") < 4){
        return false;
    }else{
        return true;
    }
    return false; // cant find email field..
}



remove space in that @ thing, the commenting system turns that whole block of code into an email address link without..

by
by
by
by
by
by