Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2003
    Location
    Bournemouth
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Javascript Alerts

    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

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Too many parinthesis! Also, wrong IF order. Check below, see if you can understand the changes.

    Code:
    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;
    }

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, javascript doesn't use english-word operators (like vbscript does), it uses symbol operators

    &&, not AND
    Code:
    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.
    Last edited by beetle; 04-02-2003 at 04:25 PM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    41° 8' 52" N -95° 53' 31" W
    Posts
    3,660
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    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:
    Code:
          <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
    Last edited by whammy; 04-03-2003 at 02:41 AM.
    Former ASP Forum Moderator - I'm back!

    If you can teach yourself how to learn, you can learn anything. ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •