/    Sign up×
Community /Pin to ProfileBookmark

I am trying to create a javascript page where a user enters a username and password and that username and password gets saved as a cookie. If they come back to the page later on i need them to enter the same name and password and it needs to match the name and password they put in before if they do not get it right within three tries then they are able to enter a new name and password that will be saved as a cookie as well. I have gotten as far as saving the username and password as a cookie but i do not know how to validate the name and password once it is saved as a cookie. Can anyone help?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@knivesDec 01.2005 — try to check this function

i think you need this.. when the user go to your page at the first time create a two cookie one is for the username and other one for the password

every time you reload your page try to read the cookie

function createCookie(name,value,days) {

//if you set the days to -1 the cookie will delete

//if 0 the cookie will delete when the user close the browser

if (days)

{

var date = new Date();

/*Now get the current Time (in milliseconds) and add the required number of days

*
(in milliseconds). Set the Time of the date to this new value, so that it now

*contains the date in milliseconds that the cookie should expire.*/

date.setTime(date.getTime()+(days*24*60*60*1000));

/*Set the variable expires to this date in the UTC/GMT format

*
required by cookies.*/

var expires = "; expires="+date.toGMTString();

}

else var expires = "";

//Finally write the new cookie intodocument.cookie in the correct syntax

document.cookie = name+"="+value+expires+"; path=/";

}

function readCookie(name)

{//We're going to search for the name of the cookie, followed by an "=".

//So create this new string and put it in nameEQ:

var nameEQ = name + "=";

/* Then split document.cookie on semicolons. "ca" becomes an array

containing all cookies that are set for this domain and path.*
/

var ca = document.cookie.split(';');

/*Then we go through the array (so through all cookies)*/

for(var i=0;i < ca.length;i++)

{

var c = ca[i];/*Set "c" to the cookie to be checked.*/

/*If the first character is a space, remove it by using the

substring() method. Continue doing this until

the first character is not a space*
/

while (c.charAt(0)==' ') c = c.substring(1,c.length);

//now we found what are looking for we have to return it

if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);

}

/*If, after having gone through all cookies, we haven't found the name we're

looking for, the cookie is not present. We return null.*
/

return null;

}
Copy linkTweet thisAlerts:
@danja5485authorDec 01.2005 — I think that is a little different from what i need i have this so far:

<!DOCTYPE html PUBLIC

"-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<head>

<title></title>



<script type="text/javascript">

<!-- HIDE FROM INCOMPATIBLE BROWSERS

function setCookie() {

var expiresDate = new Date()

expiresDate.setFullYear(expiresDate.getFullYear() + 1);

document.cookie = encodeURI("user_name =" + document.forms[0].user_name.value) + ";expires =" + expiresDate.toUTCString();

document.cookie = encodeURI("password =" + document.forms[0].password.value) + ";expires =" + expiresDate.toUTCString();

window.alert("Your name and password have been saved in a cookie.");

user_name = ""

password = ""

}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->

</script>

</head>

<body>



<script type="text/javascript">

<!-- HIDE FROM INCOMPATIBLE BROWSERS



// STOP HIDING FROM INCOMPATIBLE BROWSERS -->

</script>

<form action = "">

<p>Name: <input type = "text" name = "user_name" /> <br />

Password: <input type = "text" name = "password" /></p>

<p><input type = "button" value = "Submit" onclick = "setCookie();" /> </p>

</form>

</body>

</html>

now from this i need to somehow create a code to verify that user_name and password are the same as the original user_name and password the user enters the first time they visit the page. If it is not the same after they try 3 times, they must be able to reset the cookie to whatever they want it to be. Its hard to explain its from the book by Don Gosselin called "JavaScript Third Edition" on page 428 if anyone has the book ? Thanks for your help though knives.
×

Success!

Help @danja5485 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...