...

View Full Version : validation



nick2price
06-17-2010, 03:42 PM
Hi, got my form here

<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>
</form>

And that should call up this

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;
}

For some reason though the validation is not working. Is there anything obvious I am doing wrong?
cheers

Philip M
06-17-2010, 04:44 PM
It works for me. :) What exactly do you mean by "the validation is not working"?


Quizmaster: In written communication, which country's name is an acronym for "I trust and love you"?
Contestant: Germany.

nick2price
06-17-2010, 04:53 PM
If i enter nothing into the form, and click enter, it goes through to the next page without giving me an error.

Philip M
06-17-2010, 04:58 PM
Sorry, I do not encounter that. Pressing the "Enter" key has no effect unless the validation routine has already been called, in which case it is called (properly) again.

You have onsubmit="return validateFormOnSubmit1(this)" . That will work with any trigger for the submit. So if the form is completed then pressing the enter key will submit the form.

nick2price
06-17-2010, 05:25 PM
Ok, this is what happens. I see the form on my webpage. I enter nothing at all. I click on the submit button. In doing this, I get taken to sendEmail.php. I thought that if i dont enter any data, I should get

if (fld.value == "") {
fld.style.background = 'Yellow';
error = "You didn't enter an email address.\n";
}

I dont get any of this.

Philip M
06-17-2010, 05:31 PM
You seem to be confused between the submit button (Send) and the enter key on the keyboard.

But when I click "Send" with a blank unfilled form I get an alert:-
Some fields need correction
You did not enter an email address
You did not enter an email address

I see no reason why you should not get the same.

nick2price
06-17-2010, 06:08 PM
Ok, maybe it is working, but the way I might of done things may have caused a problem. Let me explain. That js code is in its own file. This file is in the same folder as the html form. All I do to the html form is add that part I have already shown you. For other things, I have things like

<script src="../../Scripts/swfobject_modified.js" type="text/javascript"></script>

Do I need something like this for what I am using?

cheers

Old Pedant
06-17-2010, 07:54 PM
Maybe you can give us the URL of the page with the problem?

Could be something else entirely.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum