Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts

    Form validation problem.

    Hi, I'm trying to have a form validation and i can't get something to work.
    Code:
    <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.
    Code:
    <div id="error">
    <script>
    document.write(ErrMessage);
    </script>
    </div>
    And What it writes is: "Undefined"

    Can anyone help me please?

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    try using an alert

    document.write is no good for this requirement

    Code:
    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;
    }
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    But i really want it to be written in the page... Is there any other way i could do it?

  • #4
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    document.getElementById('error').innerHTML=ErrMessage.join('<br>'));
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #5
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    It still doesn't work... here is my whole code:
    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

  • #6
    Regular Coder PremiumBlend's Avatar
    Join Date
    Apr 2006
    Location
    Marion, Iowa
    Posts
    201
    Thanks
    0
    Thanked 13 Times in 13 Posts
    It seems to work just fine for me.
    My Website: DumpsterDoggy

  • #7
    Senior Coder BarrMan's Avatar
    Join Date
    Feb 2005
    Location
    Israel.
    Posts
    1,644
    Thanks
    69
    Thanked 83 Times in 82 Posts
    Hmm, What does it write to you?
    Does it write the error message on the screen instantly?

    Thanks for the reply.

  • #8
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!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>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •