...

View Full Version : Javascript Alerts



bexley
04-02-2003, 01:39 PM
Hi,
I build a password protection thing for my site. And at the moment I have some alerts for when you leave either the Username blank or the Password blank.
I also want it to have an alert for when both the username and password are blank.
I have this code below, but it doesnt seem to work, it keeps telling me that I have an object missing.
Has anyone got any ideas or do you reckon I should leave it without both being blank.

function validate(){
if (document.form1.username.value == ""){
alert("Please Enter a Username");
return false; }

if (document.form1.userpwd.value ==""){
alert("Please Enter a Password");
return false;}

if (document.form1.username.value == "") AND (document.form1.userpwd.value =="") {
alert("Please Enter a Username");
return false; }


return true;
}


Cheers

Choopernickel
04-02-2003, 02:08 PM
Too many parinthesis! Also, wrong IF order. Check below, see if you can understand the changes.


function validate(){
if (document.form1.username.value == "" AND document.form1.userpwd.value =="") {
alert("Please Enter a Username");
return false; }
if (document.form1.username.value == ""){
alert("Please Enter a Username");
return false; }
if (document.form1.userpwd.value ==""){
alert("Please Enter a Password");
return false;}
return true;
}

beetle
04-02-2003, 03:23 PM
Also, javascript doesn't use english-word operators (like vbscript does), it uses symbol operators

&&, not AND
function validate(){
var f = document.form1;
if (f.username.value == "" && f.userpwd.value =="") {
alert("Please Enter a Username");
return false; }
if (f.username.value == ""){
alert("Please Enter a Username");
return false; }
if (f.userpwd.value ==""){
alert("Please Enter a Password");
return false;}
return true;
}Although, I think checking for both is kinda silly, especially since you are delivering the same error message. Since the script will always check the username first, it has to be filled in before validation continues to the password.

Also, creating a reference to the form will make your coding easier and your script quicker.

whammy
04-03-2003, 01:37 AM
http://www.solidscripts.com/jslogin/login.htm

username: coding
password: forums

This uses Borgtex's script (the most secure javascript login I've seen)...

to accomplish what you're trying to do, I just modified his script like this in the head:



function authPass(u,p) {
if (u && p) self.location = "auth.htm?" + u + p;
return false;
}


the part "if (u && p)" makes sure that neither value is null or undefined...

... and in the body:


<form id="form1" action="javascript://" onsubmit="return authPass(this.user.value,this.pass.value)">
<table>
<tr>
<td>Username: </td><td><input type="text" name="user" /></td>
</tr>
<tr>
<td>Password: </td><td><input type="text" name="pass" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Login" /></td>
</tr>
</table>
</form>


In this part, "(this.user.value,this.pass.value)" are the arguments passed to the "authPass" function - this.user.value becomes 'u', and this.pass.value become 'p'.

P.S. If you're attempting to do any kind of javascript login, I would definitely recommend borgtex's since nothing in the source code can be reverse-engineered, and the only way a password will get stored in the browser's cache is if someone using that computer already knows the password.

http://www.solidscripts.com/displayscript.asp?sid=15



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum