Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

no isset 1

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
GB
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>
 
Because "myerror" is an Array, you can test its "length" property:

Code:
if (!myerror.length) {
   // do something here
}

This is effectively identical to:

Code:
if (myerror.length == 0) {
   // do something here
}

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top