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?
<!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
Bookmarks