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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate for blank fields not working 1

Status
Not open for further replies.

Katie6

Programmer
Jun 12, 2007
58
GB
Hi there,
I'm trying to make some javascript that will alert a user if any required fields have been left blank on submit.

I got the code from this website:


and have tried to put it in the html as follows:

Code:
<html>
<head>
<script language="javascript" type="text/javascript">
function validate() {
with (form1) {
var alertMessage = "The following REQUIRED fields\nhave been left empty:\n";
if (Number.value == "") {
alertMessage += "\Number" }
if (Letter.value == "") {
alertMessage += "\Letter" }
if (alertMessage != "The following REQUIRED fields\nhave been left empty:\n") {
alert(alertMessage);
return (false);
}
return (true)
}
}
</script>
</head>

<form name="form1" onsubmit=" return validate()" >
<label for="Number">Number:</label> 
<select name="Number" selected="" >
<option value=""></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

<label for="Letter">Letter:</label> 
<select name="Letter">
<option value=""></option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>

<input type="button" value="Submit" >
</form>
</html>

But it doesn't work at all. Does anyone know where I have gone wrong?

Many thanks,

Katie
 
[1]
><input type="button" value="Submit" >
[tt]<input type="submit" value="Submit" >[/tt]
ps: don't make value "submit", "Submit" is barely okay. Just make sure later you don't change it to lower-letter.
[2]
>with (form1) {
[tt]with (document.form1) {[/tt]

[3] The way you use alert message is unnecessarily twisted. However, each has their own style to script.
 
Thanks! Upon re-read, I over-react on [1]. You can set value to whatever you want, even "submit". My opinion (ps)there is off the point. Please ignore it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top