Hi
I am having a problem with the code below. If I insert text into the two fields, I get a empty box appear on the screen. The reason is because the myerror variable/array has been defined. I am new to javascript, in PHP I could have used the isset to check the variable. From what I can find on the net, there is no isset in Javascript. Please could you point me in the right direction. Here is the code.
Thanks very much in advance.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function validate_form() {
var myerror = new Array();
valid = true;
if(document.getElementById("first_name").value == "") {
myerror[0] = "Enter your firstname\n";
valid = false;
}
if(document.getElementById("last_name").value == "") {
myerror[1] = "Enter your lastname\n";
valid = false;
}
if(typeof myerror != 'undefined') {
//var test = myerror.sort("\n");
alert(myerror.join(""));
valid = false;
}
return valid;
}
</script>
</head>
<body>
<form action="test.html" method="post" onsubmit="return validate_form();" />
<p>Your First Name: <input type="text" name="first_name" /></p>
<p>Your Last Name: <input type="text" name="last_name" /></p>
<p><input type="submit" name="send" value="Send Details" /></p>
</form>
</body>
</html>
I am having a problem with the code below. If I insert text into the two fields, I get a empty box appear on the screen. The reason is because the myerror variable/array has been defined. I am new to javascript, in PHP I could have used the isset to check the variable. From what I can find on the net, there is no isset in Javascript. Please could you point me in the right direction. Here is the code.
Thanks very much in advance.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<html xmlns="<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function validate_form() {
var myerror = new Array();
valid = true;
if(document.getElementById("first_name").value == "") {
myerror[0] = "Enter your firstname\n";
valid = false;
}
if(document.getElementById("last_name").value == "") {
myerror[1] = "Enter your lastname\n";
valid = false;
}
if(typeof myerror != 'undefined') {
//var test = myerror.sort("\n");
alert(myerror.join(""));
valid = false;
}
return valid;
}
</script>
</head>
<body>
<form action="test.html" method="post" onsubmit="return validate_form();" />
<p>Your First Name: <input type="text" name="first_name" /></p>
<p>Your Last Name: <input type="text" name="last_name" /></p>
<p><input type="submit" name="send" value="Send Details" /></p>
</form>
</body>
</html>