bueno tengo un problemin aki tenog un formulario en una web y mi profesor kiere q cada input en el form se e guarde en un cookie. mas o menos tengo el ejemplo q el nos dio pero lo hizo desd el mismo html, metio el script ahi ahora en mi projecto yo tengo q hacer todo desd un js externo. tengo q validar cada field y guardarlo en una cookie para cada vez q entre ahi aparescan lso datos en el form.
aki tengo el script hasta el momento, es largo y yo se q hay forma mas faciles para hacer las validacions pero no puedo usarlos porq no he visto regular expresions, asi q me hize un codigo mero loco pero sirve.
// open a new window
var win_pop;
function openWindow(url, features)
{
win_pop = window.open(url,"my_window",features);
win_pop.focus();
return win_pop;
}
//show the image in a new open windows
//added 30 to the height and 18 to the width to center the image to the window
function showImage()
{
openWindow('','width=335, height=428, top=400, left=400');
win_pop.document.write("<img src='images/webexample.jpg' alt='Tom\'s Plumbin Website' title='Website de Tom\'s Plumbin' width='317' height='398' /> <br> <a href'#' onclick='window.close()' style='color: 0ff; align: center; '>Close</>");
}
//increase the size of the fonts
function increaseFontSize()
{
var paras = document.getElementsByTagName('p');
for (i=0; i<paras.length; i++)
{
paras[i].style.fontSize = '16px';
}
}
// decrease the size of the fonts
function decreaseFontSize()
{
var paras = document.getElementsByTagName('p');
for(i=0;i<paras.length;i++)
{
paras[i].style.fontSize = '10px';
}
}
// medium size of the fonts
function mediumFontSize()
{
var paras = document.getElementsByTagName('p');
for(i=0;i<paras.length;i++)
{
paras[i].style.fontSize = '14px';
}
}
//validating the form's inputs
function isFormvalid(contactform)
{
var first_name = contactform.first_name.value;
var last_name = contactform.last_name.value;
var email = contactform.email.value;
var subject = contactform.subject.value;
var telephone = contactform.telephone.value;
var address = contactform.address.value;
var postalcode = contactform.postalcode.value;
var url = contactform.url.value;
var age = contactform.age.value;
var comments = contactform.comments.value;
if ( first_name == "") {
alert('Please enter your first name');
contactform.first_name.focus();
return false;
}
if ( last_name == "") {
alert('Please enter your last name');
return false;
}
if (email == "") {
alert('Please enter an email address');
return false;
}
else
{
var amphora = "@" //declaring the amphora
var period = "." // declaring the period
var amphora_position = email.indexOf(amphora); //getting the amphora position
var period_position = email.indexOf(period); //getting the period position
/* validate email address. Search for amphora and period in the email
address and if they are not found show a message and ask for the email again
until it's valid */
if(amphora_position == -1 || period_position == -1)
{
alert("Your Email appears to be invalid");
return false;
}
}
if (subject == "") {
alert('Please enter a subject');
return false;
}
if (telephone == "") {
alert('Please enter a your telephone number');
return false;
}
else
{
telephone_length = telephone.length;
if(telephone_length != 10)
{
alert('Your telephone number must have 10 digits example 2049554345');
return false;
}
else
{
for (i=0; i <telephone.length; i++)
{
var slots=telephone.charAt(i);
slots = parseInt(slots);
if(isNaN(slots))
{
alert('Your telephone number is not valid, enter only digits numbers');
return false;
}
}
}
}
if (address == "") {
alert('Please enter an address');
return false;
}
if (postalcode == "") {
alert('Please enter your postal code');
return false;
}
else
{
// check for a valid canadian postal code
//get the postal code length
//convert the lowercases to uppercase
postalcode=postalcode.toUpperCase();
letter1= 'ABCEGHJKLMNPRSTVXY';
letter2= letter1+'WZ';
number3= '0123456789';
if(letter1.indexOf(postalcode.charAt(0))<0)
{
alert('Your postal code is not valid');
return false;
}
if(number3.indexOf(postalcode.charAt(1))<0)
{
alert('Your postal code is not valid');
return false;
}
if(letter2.indexOf(postalcode.charAt(2))<0)
{
alert('Your postal code is not valid');
return false;
}
if(number3.indexOf(postalcode.charAt(3))<0)
{
alert('Your postal code is not valid');
return false;
}
if(letter2.indexOf(postalcode.charAt(4))<0)
{
alert('Your postal code is not valid');
return false;
}
if(number3.indexOf(postalcode.charAt(5))<0)
{
alert('Your postal code is not valid');
return false;
}
}
//valid url
if(url != "")
{
var protocol = "http://";
var host = "www.";
var period = ".";
var protocol_position = url.indexOf(protocol);
var host_position = url.indexOf(host);
var period_position = url.indexOf(period);
var first_period = url.charAt(url.length-1) ;
var second_period = url.charAt(url.length-2);
var third_period = url.charAt(0);
var periodvalidfirst = url.charAt(url.length-3);
var periodvalidsecond = url.charAt(url.length-4);
if(protocol_position == -1 || host_position == -1 || first_period == period || second_period == period || third_period == period)
{
alert("Your Url is not valid please follow this format http://www.domain.com ");
return false;
}
if(periodvalidfirst == period || periodvalidsecond == period )
{
if (comments == "")
{
alert('Please enter your comments');
return false;
}
}
else
{
alert("Your Url is not valid please follow this format http://www.domain.com ");
return false;
}
}
if (age != "")
{
//check if the age is valid
for (i=0; i <age.length; i++)
{
var slots=age.charAt(i);
slots = parseInt(slots);
if(isNaN(slots))
{
alert('Your age is not valid, enter only digits');
return false;
}
}
}
}
// save the form inputs in a cookie
function setCookie()
{
// get the information from the form fields.
var first_name = contact_form.first_name.value;
var last_name = contact_form.last_name.value;
var email = contact_form.email.value;
var subject = contact_form.subject.value;
var telephone = contact_form.telephone.value;
var address = contact_form.address.value;
var postalcode = contact_form.postalcode.value;
var url = contact_form.url.value;
var age = contact_form.age.value;
var comments = contact_form.comments.value;
if (url == "" || age == "")
{
// Set the expiry date.
var expireDate = new Date();
expireDate.setMonth(expireDate.getMonth()+6);
// concatenate and escape all the information.
var cookie_contents = 'my_first_cookie=' + 'username:' + first_name + '-last_name:' + last_name + '-email:' + email + '-subject:' + subject + '-telephone:' + telephone + '-address:' + address + '-postalcode:' + postalcode + '-comments:' + comments + ';expires=' + expireDate.toGMTString();
cookie_contents = escape(cookie_contents);
// Write the cookie.
document.cookie = cookie_contents;
alert('esto es lo de la cookie');
alert(first_name);
alert(last_name);
alert(email);
alert(subject);
alert(telephone);
alert(address);
alert(postalcode);
alert(comments);
}
else
{
// Set the expiry date.
var expireDate = new Date();
expireDate.setMonth(expireDate.getMonth()+6);
// concatenate and escape all the information.
var cookie_contents = 'my_first_cookie=' + 'username:' + first_name + '-last_name:' + last_name + '-email:' + email + '-subject:' + subject + '-telephone:' + telephone + '-address:' + address + '-postalcode:' + postalcode + '-url:' + url + '-age:' + age + '-comments:' + comments + ';expires=' + expireDate.toGMTString();
cookie_contents = escape(cookie_contents);
// Write the cookie.
document.cookie = cookie_contents;
alert('esto es lo de la cookie con url y age');
alert(first_name);
alert(last_name);
alert(email);
alert(subject);
alert(telephone);
alert(address);
alert(postalcode);
alert(url);
alert(age);
alert(comments);
}
}
function readCookie()
{
// If the cookie is not empty.
if(document.cookie != ''){
var a_pair;
var user_info = new Array();
// Put the contents of the cookie into the variable, the_cookie.
var the_cookie = document.cookie;
// unescape the_cookie.
the_cookie = unescape(the_cookie);
// Use split() to separate the name of the cookie from the rest of it.
var split_cookie = the_cookie.split('=');
// The second part of the array is the part we want.
var cookie_stuff = split_cookie[1];
// Use split() to separate the expiry date from the rest of the cookie.
var no_expiry = cookie_stuff.split(';');
// Now it's the first part of this bit that we want.
var the_info = no_expiry[0]
// Split the_info into the three name:value pairs.
var cookie_info = the_info.split('-')
for(i=0; i < cookie_info.length; i++) {
// Get a pair from the array.
var a_pair = cookie_info[i];
// split the pair up using the hyphen delimeter.
var split_pair = a_pair.split(':');
// Get the first part of the pair
var the_name = split_pair[0];
// Get the second part of the pair.
var the_value = split_pair[1];
// Load up an associative array with those values.
user_info[the_name] = the_value;
}
}
// If the cookie is empty, let the user know.
else {
name = '(No cookie set yet.)';
}
}
ahi tengo lo de validar el form, asi como tambien abrir una nueva ventana con imagen y cambiar el font de el text... pero lo mas importante es lo de las cookies me da un error q sale indefinido
function setCookie()
{
// get the information from the form fields.
var first_name = contact_form.first_name.value;
var last_name = contact_form.last_name.value;
var email = contact_form.email.value;
var subject = contact_form.subject.value;
var telephone = contact_form.telephone.value;
var address = contact_form.address.value;
var postalcode = contact_form.postalcode.value;
var url = contact_form.url.value;
var age = contact_form.age.value;
var comments = contact_form.comments.value;
es en esta parte, esto me sale indefinido
contact_form.first_name.value
(ya termino mi proyecto y ya lo resolvi

.. ) pero pues talvez le sirve aalguien d algo eso
document.getelementbyid('contact_forrm').first_name.value
asi es como se resolvia