I know that the best way to make a log-in is by using PHP, but since I still don't have an FTP program (please don't give me links or anything, only pay attention to my questions please), I can't use it. I want the javascript to validate one textfield (for now, since I can add a password one later). I have an array of the usernames, and what I did was use a for statement to go to let's say 100, then in the for statement I had an if statement, which basically says:
for (i=0,i<=100,i++) {
if (textFieldName==myArray[i]) {
alert("Welcome")
} else {
alert("Wrong")
}
}
It doesn't work, and I don't know why. I'm knew to javascript, so it would be awsome if you ppl could help me!
I might use an object instead of an array, it'll save on the looping. Here's an example:
Code:
<html>
<head>
<script type="text/javascript">
onload = function() {
// setup the list of valid usernames
var users = {
"pazil": 1,
"catbert303": 1,
"some_other_user": 1
};
// have the script check if the username entered is valid when the user leaves the textfield
document.getElementById('username').onblur = function() {
// if the value in the textfield is in the users lookup object then they entered a correct name
if (users[this.value]) {
alert('welcome');
} else {
alert('wrong');
}
};
};
</script>
</head>
<body>
<input type="text" id="username" value="">
</body>
</html>
OK, thanks! I still have 3 questions. Why do the var names have : 1, at the end? And how would I attach a password to each username? Also, the reason I wanted to use an array, was then for registry, I just add their wanted username and password to the two username and password arrays, then it saves the javascript file...So how would I make a registry for this way? Thanks alot though!
the 1's are there simply so that when the property is checked in the lookup table it is found to exist.
If you want to add passwords to this (again remembering that this offers zero security!) you would replace the 1's with the passwords for the individual usernames, here's a very quick example:
Code:
<html>
<head>
<script type="text/javascript">
onload = function() {
// setup the list of valid usernames
var users = {
"pazil": "password",
"catbert303": "another password",
"some_other_user": "wow, would you look at that - a password"
};
document.getElementById('login').onclick = function() {
var user = document.getElementById('username').value;
var pass = document.getElementById('password').value;
// if the value entered for the username exists in the lookup table and the password entered matches the password for that user...
if (users[user] && users[user] == pass) {
alert('welcome');
} else {
alert('wrong');
}
};
};
</script>
</head>
<body>
Username: <input type="text" id="username" value=""><br>
Password: <input type="password" id="password" value=""><br>
<input type="button" id="login" value="Login">
</body>
</html>
Thank-you so much! Do I NEED to use PHP to make a registry? Also, for security, can't I just link the js file to this document? THANK-YOU!
No. That code is still accessible to the end user. JavaScript is done client side and anything client side can be seen. To do it secure you'll have to use server side technology like ASP or PHP.
Jason L. Wright
I'm not that hard to imitate. Just make some random negative claim at Apple or anything else for that matter and then have nothing to back it up.
If you mean registration then No, JavaScript can't save anything on the server without some kind of server side language helping it.
Jason L. Wright
I'm not that hard to imitate. Just make some random negative claim at Apple or anything else for that matter and then have nothing to back it up.
Jason L. Wright
I'm not that hard to imitate. Just make some random negative claim at Apple or anything else for that matter and then have nothing to back it up.
well...bascially learning the code. I tried to learn PHP at w3schools.com, which was pretty useful, except...I can't try it out, since I don't have an FTP program...
You only need an FTP program if you have a server somewhere that supports PHP. To test on your local computer install WAMP.
Jason L. Wright
I'm not that hard to imitate. Just make some random negative claim at Apple or anything else for that matter and then have nothing to back it up.
Jason L. Wright
I'm not that hard to imitate. Just make some random negative claim at Apple or anything else for that matter and then have nothing to back it up.
"You idiot! You own a Macintosh! The file is ****ing gone! It's just gone!" That's my favourite part...anyways, cya all, and thanx for all the help ppl!!! I'll most likely post again in this forum with some other problem I have in Javascript...cya!