View Full Version : Validation

06-15-2010, 01:07 AM
Hey. Could someone point me in the direction of why this is not working, it worked in my last program, but not since I have changed things. I have a standard html form

<form method="POST" onsubmit="return validate_form(this)" action="anniversaryPreview.php">
<p class="demo5">Senders Name<a class="textFields2">
<input type="text" name="sender" value="e.g. Love from Nick" size="30" maxlength="35" /></a></p>
<p class="btn2" >
<input type="submit" value="Preview"></p>

And this should call up return validate_form(this) before bringing up the php page. Now the validator is just

function validate_required(field, alerttxt) {
if (field.value == null || field.value == "") {
return false;
else {
return true;

function validate_form(thisform) {
if (validate_required(thisform.name,"Please specify the receivers name.")==false) {
return false;
if (validate_required(thisform.sender,"Please specify the sender's name.")==false) {
return false;
(This is not in the html file, but its own seperate file)
I cant see if I have made any mistakes with the variables names or something, or what I am doing wrong. Dont get any errors, just nothing happens if I leave my forms fields blank. Anyone have an idea of whats going on here?


Old Pedant
06-15-2010, 01:53 AM

if (validate_required(thisform.name,"...

Where in that <form>, pray tell, do you have any field

<input type="text" name="name" ...>

The only text field I see there is the one named "sender".

Oh, and you *ARE* getting errors. I would assume you are not using a debugger, so you simply aren't aware of them.

I should note that your so-called validation is pretty useless. An entry of, say, a single space in the text field would be accepted by that code. Surely you should use more robust validation than that???

06-15-2010, 03:03 AM
Ok, so changes have been made. I am doing this on a different form now. The form is

<form method="POST" onsubmit="return validateFormOnSubmit1(this)" action="sendEmail.php">
<p class="demo2">Receivers E-mail<a class="textFields">
<input type="text" name="receiver" size="30" maxlength="35" /></a></p>
<p class="demo2">Senders E-mail<a class="textFields2">
<input type="text" name="sender" size="30" maxlength="35" /></a></p>
<p class="btn" ><input name="submit" type="submit" value="Send"/></p>

So that looks correct, with two fields sender and receiver. I then have another file, which holds the javascript code. This file is in the same folder as the above file. It looks like

function validateFormOnSubmit1(theForm) {
var reason = "";

reason += validateEmail1(theForm.receiver);
reason += validateEmail1(theForm.sender);

if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;

return true;

function trim(s)
return s.replace(/^\s+|\s+$/, '');

function validateEmail1(fld) {
var error="";
var tfld = trim(fld.value); // value of field with whitespace trimmed off
var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter an email address.\n";
} else if (!emailFilter.test(tfld)) { //test email for illegal characters
fld.style.background = 'Yellow';
error = "Please enter a valid email address.\n";
} else if (fld.value.match(illegalChars)) {
fld.style.background = 'Yellow';
error = "The email address contains illegal characters.\n";
} else if (fld.value.length == 0) {
fld.style.background = 'Yellow';
error = "The required field has not been filled in.\n"
} else {
fld.style.background = 'White';
return error;

So, I am using receiver and sender, which are the variables from my form. However, it still goes straight through to the php file even if I enter an incorrect or empty email. I am using Dreamweaver, so dont know if this has a debugger. But, is there anything obviously wrong with what I am doing?


06-15-2010, 03:18 AM
Whoops, submit needs a capital S

Actually, it still doesnt work for some reason.