Forum Moderators: open
Thanks in advance for any help - I'm sure this is simple, but I guess it's always simple once you know it.
I have a basic form, and am looking to validate it through javascript.
So far I have an emptyfields validation, which works perfectly. the form is defined as:
<form name="form1" method="POST" action="/cgi-bin/csvwrite.pl" onsubmit="return checkFields()" >
and the function has been defined as:
function checkFields()and so on for each field
{
emptyfields = "";
if (document.form1.forename.value == ""){
emptyfields += "\n - Forename";
}
if (document.form1.surname.value == ""){
emptyfields += "\n - Surname";
}
if (emptyfields!=""){
emptyfields = "These fields are required:\n" +
emptyfields + "\n\nPlease fill in all required fields";
alert(emptyfields);
return false;
}
else return true;
}
Ok, so far, so good. but when i look to add this code to validate emails by making sure the same email address is entered for 'email' and 'email2', it simply isn't running.
i've tried to run it as a multiple function at the submit stage, i've tried to run it as a subsection of checkfields, and i've tried to create a separate function which would run both these functions. all failed miserably, because i don't have a clue.
the function i'm looking to use is:
function compareEmails(Form1) { if (Form1.email.value!= Form1.email2.value) { alert("Your email addresses don't match. Please double check"); return false; } else { return true; } }
the action on submit was originally:
<onSubmit="compareEmails(this);">
there is one other function i wish to add;
on a date entry for a requested record, i want to restrict the year entered to 1800-1900. Again, I'm sure it's a basic validation, but I wanted to know if (A)Do I need a separate function for this - and (B) How would this limit be defined.
It's a big post for a small problem, but I figure the more information I give, the more someone might be able to help.
If anyone can help with this, it's greatly appreciated.
PS - the scripts are coming up as big long lines, despite my putting line breaks in.
[code]
function compareEmails(Form1) { if (Form1.email.value!= Form1.email2.value) { alert("Your email addresses don't match. Please double check"); return false; } else { return true; } }
[/code
[quote]
One thing to make sure you don't do is set your passed in variable to the function the same as your form name. It looks like your ok there, but thought I'd provide previous warning :).
I'm not sure I understand the previous warning, but thanks all the same :-)
I'm thinking that there's two ways to deal with this.
one is the 'onsubmit' issue; do i have one function to run from that point, and then issue separate functions within it, the empty fields and email check being two, with the possibility of more.
or...maybe i could use the 'onBlur' function to simply do this before submission. However.....since the emails are the last fields, if people simply go from the 'email2' field to submit, would it even be checked?
Any advice appreciated