"Matt Herson" <eb***@mherson.com> writes:
I am looking for a validation script that will only look at the fields in
the cgi form, determine if the fields are filled out, then if one or more
are blank will write a message at the top of the page stating that some
fields have not been filled out. I don't want a pop-up, I just want to send
them back. I had seen something that did this using <div /> tags but cant
seem to find one I can modify to work for this application.
Try something like this:
---
<script type="text/javascript">
function validate(form) {
for (var i = 0; i < form.elements.length; i++) {
var elem = form.elements[i];
if (elem.tagName == "INPUT" &&
(elem.type == "text" || elem.type == "password") ||
elem.tagName == "TEXTAREA") {
if (elem.value == "") {return false;}
}
}
return true;
}
function validateAndDisplay(form,div) {
div = document.getElementById(div);
if (!validate(form)) {
div.innerHTML = "Some fields still needs to be filled.";
return false;
} else {
div.innerHTML = "";
return true;
}
}
</script>
<div id="output"></div>
<form action="..." onsubmit="validateAndDisplay(this,'output')">
<input type="text" name="f1">
<input type="password" name="f2">
<textarea name="f3"></textarea>
<input type="submit" value="submit">
</form>
---
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'