Forum Moderators: open

Message Too Old, No Replies

Simple JavaScript Issue

JavaScript form validation

         

field4000

1:26 am on Mar 29, 2006 (gmt 0)

10+ Year Member



Hey all,

I have a simple JavaScript issue. Or what I think is a simple issue.

I pinched a script to validate a form (client-side) using JavaScript from a site.

It works nearly as I want it to. The only issue is that it doesn't display all the errors at once. I would like all the errors to display at the same time, not one at a time.

Below is my edited code:

<html>

<style>

body
{
font-family: Tahoma;
font-size: 10pt;
}

input
{
width: 200px;
font-family: Tahoma;
font-size: 10pt;
}

.label
{
width:150px;
}

textarea
{
width: 200px;
font-family: Tahoma;
font-size: 10pt;
}

.error
{
font-family: Tahoma;
font-size: 10pt;
color: red;
display:none;
}

</style>

<script>

function checkForm()
{
name = document.getElementById("name").value;­
email = document.getElementById("email").value;­
comment = document.getElementById("comment")­ .value;

if (name == "")
{
hideAllErrors();
document.getElementById("nameError­ ").style.display = "inline";
document.getElementById("name").se­ lect();
document.getElementById("name").focus();­
return false;
}

else if (email == "")
{
hideAllErrors();
document.getElementById("emailErro­ r").style.display = "inline";
document.getElementById("email").s­ elect();
document.getElementById("email").f­ ocus();
return false;
}

else if (comment == "")
{
hideAllErrors();
document.getElementById("commentEr­ ror").style.display = "inline";
document.getElementById("comment")­ .select();
document.getElementById("comment")­ .focus();
return false;
}

return true;
}

function hideAllErrors()
{
document.getElementById("nameError­ ").style.display = "none"
document.getElementById("emailErro­ r").style.display = "none"
document.getElementById("commentEr­ ror").style.display = "none"
}

</script>

<body>

<form onSubmit="return checkForm();" method=post action="xyz.php">

<span class="label">Name:<br /></span>
<input type=text value="" id="name">
<div class="error" id="nameError">Required: Please enter your name.</div>

<br /><span class="label">Email:<br /></span>
<input type=text value="" id="email">
<div class="error" id="emailError">Required: Please enter your email address.</div>

<br /><span class="label">Comment:<br /></span>
<textarea name="" cols="" rows="" id="comment"></textarea>
<div class="error" id="commentError">Required: Please enter a comment.</div>

<br /><br /><input type=submit> <input type=reset>
</form>

</body>

</html>

Anybody have any ideas?

Cheers in advance.

Dijkgraaf

4:46 am on Mar 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well you would have to replace all the lines of
return false;
with something like
isvalid=false;
You will have to initialise that at the top with
isvalid=true;
You will also want to call hideAllErrors(); there and removed it from the other places, and get rid of all the elseif's

And then at the end of the function you replace
return true;
with
return isvalid;