How can I add a cookie to the jQuery below for the first and last names on the f
ID: 3681059 • Letter: H
Question
How can I add a cookie to the jQuery below for the first and last names on the form?
//Newsletter Form Validation
$(document).ready(function() {
// move focus to first text box
$("#first_name").focus();
$("#newsletter").validate({
rules: {
email: {
required: true,
email: true
},
first_name: {
required: true,
lettersonly: true
},
last_name: {
required: true,
lettersonly: true
}
},
messages: {
first_name: {
lettersonly: " *Invalid"
},
last_name: {
lettersonly: " *Invalid"
},
email: {
email: " *Invalid "
}
}
});
});
Explanation / Answer
cookies play a crucial role in storing few variables which are accessable across all pages and for certian amount of time. After the stimualted time cookies will expire.
cookie are set using jquery as follows.
we will set various opton like expires,path and domain
expires will specifyafter how many days the cookie will expire.
secure option is set to true if we want to transmit over https protocol.
Code for the given problem with setting cookie is provided below.
$(document).ready(function() {
// move focus to first text box
$("#first_name").focus();
$("#newsletter").validate({
rules: {
email: {
required: true,
email: true
},
first_name: {
required: true,
lettersonly: true
},
last_name: {
required: true,
lettersonly: true
}
},
messages: {
first_name: {
lettersonly: " *Invalid"
},
last_name: {
lettersonly: " *Invalid"
},
email: {
email: " *Invalid "
}
}
});
$("form").on("click", function () {
$.cookie('myCookie_fname', $("#first_name").val(), { expires: 365 path:/});
$.cookie('myCookie_lastname', $("#last_name").val(), { expires: 365 path: '/'});
$.cookie('myCookie_email', $("#email").val(), { expires: 365 path: '/'});
});
});
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.