Click to See Complete Forum and Search --> : Help please


hitman
01-14-2003, 03:28 PM
Hi, I have an exam on Javascript tomorrow and I am looking at past exams papers.

Can you please help me out of this?
I know I am asking you this as if I let it all in your own hands. But I really don't know how to do it!!

This is part of multiple questions. Can you just tell me how to write the code for a simple page, starting at the new function writing (see below).

->At present, if the user clicks the “Calculate Name” button without entering
any names, the output will default to the first name in the midName array.
Write a new function called checkNames() that displays an alert to the user
if they do not enter any names. You will also need to specify where in the
code you would place a call to this function.

AdamBrill
01-14-2003, 04:57 PM
Try something like this:

function checkNames()
{
if(document.formname.inputname.value=="")
{
alert("Please enter your name");
}
}

Will that work?

hitman
01-14-2003, 05:56 PM
i tried it, it doesn't work. do i need to put something else? it just gives me a blank page.

AdamBrill
01-14-2003, 06:45 PM
Yeah, that was just the function. Try the following code:

<html>
<head>
<script language=javascript>
function checkNames()
{
if(document.form1.T1.value=="")
{
alert("Please enter your name");
return false;
}
return true;
}
</script>
</head>
<body>
<form method="POST" onsubmit="return checkNames()" name=form1>
<input type="text" name="T1" size="20">
<input type="submit">
</form>
</body>
</html>

That will check if the textbox name T1 is empty. If it is, it popups up the alert, and if it isn't it submits the form. Let me know if it still doesn't do what you want...

hitman
01-15-2003, 05:18 PM
thanks. it is what i wanted.