need validation code for contact form

Discussion in 'JavaScript' started by creative_007, Feb 19, 2007.

  1. #1
    Hi,
    I am thinking of keeping a contact/feedback form on my website and am in search of a validation code for the sections.
    I have found an e-mail validation at w3schools but since i am a complete noob at javascript, i am unable to expand it to other sections.
    If anybody can tell me where can i find the codes, or even examples of how to expand it, i will be pretty grateful
     
    creative_007, Feb 19, 2007 IP
  2. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Post your HTML form here and I will write a javascript for you :D
     
    designcode, Feb 19, 2007 IP
  3. creative_007

    creative_007 Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Designcode, thanks for your help..here's the code

    <html>
    <head>
    <title>Contact Form</title>

    </head>
    <body>
    <form action="http://www.yoursitename.com/cgi-sys/formmail.pl" name="myform" >
    <input type="hidden" name="recipient" value="email">
    <input type="hidden" name="subject" value="FormMail E-Mail">
    <table cellspacing="2" cellpadding="2" border="0">
    <tr>
    <td align="left">Name:
    <input type="text" name="Name" size="50"></td>
    </tr>
    <tr>
    <td align="left">EMail:
    <input type="text" name="Email" size="50"></td>
    </tr>
    <tr>
    <td align="left">Feedback</td></tr>
    <tr>
    <td><textarea cols="50" rows="10" name="Feedback"></textarea></td>
    </tr>
    <tr>
    <td align="left">
    <input type="submit" value="Submit"><input type="reset" value="Reset">
    <input type="hidden" name="redirect" value="thankyou.html"></td>
    </tr>
    </table>
    </form>
     
    creative_007, Feb 19, 2007 IP
  4. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #4
    Here is your code with JS validation :D

    
    <html>
    <head>
    <title>Contact Form</title>
    
    </head>
    <body>
    <script type="text/javascript">
    
    function validateForm()
    {
    	var f = document.myform;
    	if(f.Name.value == "")
    	{
    		alert("Please enter your name");
    		f.Name.focus();
    		return false;
    	}
    	else if(!checkEmail(f.Email.value))
    	{
    		alert("Please enter your email address");
    		f.Email.focus();
    		return false;
    	}
    	else if(f.Feedback.value == "")
    	{
    		alert("Please enter some message");
    		f.Feedback.focus();
    		return false;
    	}
    }
    
    function checkEmail(str)
    {
    	// Function from SmartWebby.com, modified by designcode
    	if(str == "")
    		return;
    	
    	var at="@"
    	var dot="."
    	var lat=str.indexOf(at)
    	var lstr=str.length
    	var ldot=str.indexOf(dot)
    	if (str.indexOf(at)==-1)
    	return false;
    	
    	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
    		return false;
    	
    	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
    		return false;
    	
    	if (str.indexOf(at,(lat+1))!=-1)
    		return false;
    	
    	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
    		return false;
    	
    	if (str.indexOf(dot,(lat+2))==-1)
    		return false;
    	
    	if (str.indexOf(" ")!=-1)
    		return false;
    	
    	return true;
    }
    </script>
    <form action="http://www.yoursitename.com/cgi-sys/formmail.pl" onsubmit="return validateForm();" name="myform" >
    <input type="hidden" name="recipient" value="email">
    <input type="hidden" name="subject" value="FormMail E-Mail">
    <table cellspacing="2" cellpadding="2" border="0">
    <tr>
    <td align="left">Name:
    <input type="text" name="Name" size="50"></td>
    </tr>
    <tr>
    <td align="left">EMail:
    <input type="text" name="Email" size="50"></td>
    </tr>
    <tr>
    <td align="left">Feedback</td></tr>
    <tr>
    <td><textarea cols="50" rows="10" name="Feedback"></textarea></td>
    </tr>
    <tr>
    <td align="left">
    <input type="submit" value="Submit"><input type="reset" value="Reset">
    <input type="hidden" name="redirect" value="thankyou.html"></td>
    </tr>
    </table>
    </form>
    
    Code (markup):
     
    designcode, Feb 19, 2007 IP
  5. creative_007

    creative_007 Active Member

    Messages:
    318
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Designcode, Thanks so much for your prompt help..I am very grateful.
     
    creative_007, Feb 19, 2007 IP
  6. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #6
    Please add to my reputation :D and I will be grateful to you :D
     
    designcode, Feb 19, 2007 IP