Help needed with JS functions in xhtml

Trez

Solid State Member
Messages
8
(this is for college) Basically, i've reated a form, now I have to add Javascript to the form. Its a basic survey form, the first question in the form is displayed through radio buttons. I have to add Javascript to this bit, the user must answer this question, if they don't (ie miss it) and answer the question below, then they must be alerted, can anyone help with how to do this?

Thanks :)
 
just off the top of my head...

all you need to do is pass the name of the previous element to a function so that it can check that is have data...

Code:
<head><script type="text/javascript">
function check_box(element)
{
if (element == "name") { if (document.form1.name.value == "") { alert("you must fill in your name")} } 
if (element == "address") { if (document.form1.address.value == "") { alert("you must fill in your address")} }
}	
</script>
</head>
<body>
<form name="form1">
name:<input type="text" name="name"  />
address:<input type="text" name="address" onclick="check_box('name')" />
postcode: name:<input type="text" name="postcode" onclick="check_box('address')" />
<input type="submit">
</form>

there are a lot more ways to do this that are better,
for instance checking the form as a whole when it is submitted, that way the user can skip questions and come back later.
 
Back
Top Bottom