Hi,
Using Dreamweaver cs4 on a Win7 pc.
I am aware that form validation has probably been done to death and that the answer to my question is probably blatantly obvious, but after several hours of experimenting and searching, I've finally given up! So here goes...
I am experimenting with form validation. I have some basic code that works, but when I try to put it into any sort of function, it stops working, eg
here is what works:
Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<HEAD>
<TITLE>test form1</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function validate() {
if (document.Subscribe.Name.value.length < 1) {
alert("Please enter a value for name");
setTimeout(function() {document.Subscribe.Name.focus(); }, 10);
return false;
}
if (document.Subscribe.Email.value.length < 1) {
alert("Please enter an Email Address.");
return false;
}
return true;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name="Subscribe" Action="resultspage.asp" onSubmit="return validate();">
name:<input type="text" name="Name"><br>
email:<input type="text" name="Email"><br> <input type=submit value="Submit">
</FORM>
</BODY>
</HTML>
If I change the code to add my isDEmpty function and call it from my validate function, then everything stops working.
Code:
function isDEmpty(elName, niceName){
if (elName.value.length < 1) {
alert("Please enter a value for " + niceName);
setTimeout(function() {elName.focus(); }, 10);
return true;
}
}
function validate() {
if isDEmpty(document.Subscribe.Name, "Customerrrr Name") {
return false;
}
Can anybody explain to me what I am doing wrong? I guess a basic understanding of Javascript would help, but at the moment I am just hacking about trying to get my head around the syntax and how it all goes together... you gotta start somewhere, right?
Thanks in advance for any help.
Dan