www.webdeveloper.com
Results 1 to 5 of 5

Thread: javascript validation & cookie

Hybrid View

  1. #1
    Join Date
    Mar 2012
    Posts
    21

    javascript validation & cookie

    Hi!

    I am creating a basic form with validation and storing a cookie for 6 months. I managed majority of it but don't know where I'm going wrong.

    Any help is appreciated!

    My code so far is:
    Code:
    <html>
    
    <head>
    
    <script type="text/javascript">
    
    //validate the form (name shouldn't be blank)
    
    function validate() 
    {
    if (document.myform.Name.value=="" || null ) 
    {
    alert("Please enter your name");
    return false;
    if (document.myform.Name.value !=="" || null )
    return true;
    }
    
    //If a name is entered then the value will be set to true.//
    
    
    var email=document.myform.Email.value;
    var at=email.indexOf("@");
    var dot=email.lastIndexOf(".");
    if (at<1 || dot<at+2 || dot+2>=email.length)
    {
      alert("Please enter a valid e-mail address");
      return false;
    } 
    
    
    
    var dob = document.myform.DateOfBirth.value;
    var val = /^\d{2}\/\d{2}\/\d{4}$/;
    if (!val.test(dob))
    {alert("Please enter a valid date of birth"); return false;}
    }
    
    
    function Cookie()
    {
    var x = new Date();
    
    x.setMonth( x.getMonth() + 6 );
    
    
    namecookie= escape(document.myform.Name.value) + ""
    /
    document.cookie= name + "=" + namecookie + "; expires=" +x.toGMTString() + ";"
    
    if(document.cookie)
    {
    alert("A cookie has been created with the name " + document.cookie + " and it will expire on " + x.toGMTString());
    }
    }
    
    </script>
    
    
    
    <FORM METHOD="get"  NAME="myform" onsubmit="">
    
    <!--create text fields/tickbox-->
    
    Name: 	<br><input type="text" name="Name" value="" size="30" maxlength="50" /> <br><br>
    	
    Email: 	<br><input type="text" name="Email" value="" size="30" maxlength="50" /> <br><br>
       
    Gender: <br><input type="text" name="Gender" size="30" maxlength="6"> <br><br>
    	
    Date Of Birth (DD/MM/YYYY): <br><input type="text" name="DateOfBirth" size="30" maxlength="10"> <br><br>
    
    Please tick the box if you would like to be contacted for other marketing purposes. <INPUT TYPE="checkbox" NAME="check" VALUE="Tick">
    <br><br>
    
    <input type="submit" value="Sign Up" onclick="validate(this.form); Cookie();"/>													<!--submit button-->
    
    
    
    </html>

    At the moment I'm only wanting to store the cookie for 6 months nothing else, can anyone help me?

  2. #2
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    Check this out!
    HTML Code:
    <!doctype html>
    <html>
    <head>
        <title>Form</title>
    </head>
    <body>
        <form method="get" name="myform" onsubmit="return Validate();">
            Name: <br/> <input name="Name" size="30" maxlength="50" /> <br/> <br/>
            Email: <br/> <input name="Email" size="30" maxlength="50" /> <br/> <br/>
            Gender: <br/> <input name="Gender" size="30" maxlength="6" /> <br/> <br/>
            Date Of Birth (DD/MM/YYYY): <br/> <input name="DateOfBirth" size="30" maxlength="10" /> <br/> <br/>
            Please tick the box if you would like to be contacted for other marketing purposes! <input type="checkbox" name="check" value="Tick"> <br/> <br/>
            <input type="submit" value="Sign Up" />
        </form>
    
        <script>
            function Validate() {
                // Validating Name \\
                if (document.myform.Name.value == "" || null) { 
                    alert("Please enter your name!");
                    document.myform.Name.focus();
                    return false;
                }
            
                // Validating Email \\
                var email = document.myform.Email.value;
                var at = email.indexOf("@");
                var dot = email.lastIndexOf(".");
                if (at<1 || dot<at+2 || dot+2>=email.length) {
                    alert("Please enter a valid e-mail address!");
                    document.myform.Email.focus();
                    return false;
                }
    
                // Validation for Gender missing! \\
                
                // Validating Date Of Birth \\
                var dob = document.myform.DateOfBirth.value;
                var val = /^\d{2}\/\d{2}\/\d{4}$/;
                if (!val.test(dob)) {
                    alert("Please enter a valid date of birth"); 
                    document.myform.DateOfBirth.focus();
                    return false;
                }
                
                return Cookie(); // calling the function Cookie() after validating all form fields!
            }
    
            function Cookie() { // this function will be called only if Validate() returns true!
                var x = new Date();
                x.setMonth(x.getMonth()+6);
                var namecookie = escape(document.myform.Name.value) + "";
                document.cookie = name + "=" + namecookie + "; expires=" + x.toGMTString() + ";" ;
    
                if (document.cookie) {
                    alert("Congratulations! A cookie has been created with the name " + document.cookie + " and it will expire on " + x.toGMTString() + "!");
                    return true;
                }
            }
        </script>
    </body>
    </html>
    There is still a lot of space for improvement in this form!
    Last edited by TheAliveWinner; 05-29-2012 at 12:19 AM. Reason: I love cats!
    "It will never rain roses: when we want to have more roses, we must plant more roses." - George Eliot

  3. #3
    Join Date
    May 2012
    Location
    United State
    Posts
    79
    Nice answer,
    Thanks for help for also me. I'm also finding the same question's answer.
    Thanks buddy.

  4. #4
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    Some points to be noted are:

    Always place the validating function below the form!

    Always validate (W3C Markup Validation Service) your html code before posting here! Your code contained many errors!

  5. #5
    Join Date
    Feb 2012
    Location
    Bokaro Steel City (Jharkhand), India!
    Posts
    194
    Compare my code with yours to catch the errors!
    Ask, if you still need help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles