...

View Full Version : Form validation problem.



BarrMan
07-09-2006, 04:06 PM
Hi, I'm trying to have a form validation and i can't get something to work.


<script>
var ErrMessage;
function Validate(){
if(document.contact.email.value.length < 3){
ErrMessage = "You have entered an invalid email";
}
}
</script>

Then inside a div i'm trying to write the error.

<div id="error">
<script>
document.write(ErrMessage);
</script>
</div>
And What it writes is: "Undefined"

Can anyone help me please?

vwphillips
07-09-2006, 04:31 PM
try using an alert

document.write is no good for this requirement


function Validate(){
var ErrMessage=[];
if(document.contact.email.value.length < 3){
ErrMessage.push("You have entered an invalid email");
}
if (ErrMessage.length>0){
alert(ErrMessage.join('\n'));
return false;
}
return true;
}

BarrMan
07-09-2006, 04:41 PM
But i really want it to be written in the page... Is there any other way i could do it?

vwphillips
07-09-2006, 05:06 PM
document.getElementById('error').innerHTML=ErrMessage.join('<br>'));

BarrMan
07-10-2006, 05:48 AM
It still doesn't work... here is my whole code:

<html dir="rtl">
<head>
<title>
סתם
</title>
<style>
#Error, #Error2{
visibility: hidden;
color: red;
}
</style>
<meta http-aquiv="content-type" content="text/html" charset="windows-1255">
</head>
<body background="http://planet.nana.co.il/zigfrid/maple%5Fstory.jpg">
<script language="javascript">

function Validate(){
var Err;
if(document.contact.Password.value.length < 3){

//alert(ErrMessage)
document.all.Error.style.visibility = "visible";
contact.Password.select();
Err = "You Stupid Idiot";
}
else{
document.all.Error.style.visibility = "hidden";
}
if(document.contact.Password.value == "bar123"){
//window.location.href = "http://www.maple.10x.co.il"


}
else{
document.all.Error2.style.visibility = "visible";
contact.Password.select();
}
return Err;
}
//‎ErrMessage = Err;
//alert(ErrMessage);
</script>

Test><br>
<form name="contact" action="contact.htm" method="post">
סיסמה: <input name="Password" type="password" onBlur="Validate()">
<div id="Error">
<script language="javascript">
document.getElementById('error').innerHTML=Err;
</script></div>
<div id="Error2">Error: The password is incorrect.</div>
<br><br>
<input type="button" value="Send" onClick="Validate()">
</form>
</body>
</html>

Thanks

PremiumBlend
07-10-2006, 05:59 AM
It seems to work just fine for me.

BarrMan
07-10-2006, 08:04 AM
Hmm, What does it write to you?
Does it write the error message on the screen instantly?

Thanks for the reply.

vwphillips
07-10-2006, 01:17 PM
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
function Validate(){
document.getElementById('error').innerHTML='';
var ErrMessage=[];
if(document.contact.email.value.length < 3){
ErrMessage.push("You have entered an invalid email");
}
if (ErrMessage.length>0){
document.getElementById('error').innerHTML=ErrMessage.join('<br>');
return false;
}
return true;
}
/*]]>*/
</script>

</head>

<body>
<form name=contact >
<input name=email />
<input type=button value=Validate onclick="Validate();"
</form>

<div id="error">
</div>
</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum