Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if...else statements

Status
Not open for further replies.

light742

Technical User
Aug 9, 2010
15
US
Hello:

I am a newbie to JavaScript and just started to read a
Javascript book that is about 5 years old. I have also been on the w3schools website. Below is coding that once you load it into Internet Explorer 7, it simply asks you for the user ID and password and is designed to just give you an error message when you do not put the correct used ID and password in. This portion works properly. When you put in the correct user ID and password, you should get an alert statement that states "Logon valid," but instead states "Logon invalid." I have Windows Vista. I tried to adjust the code by adding 2 semicolons but that did not help. The code is below.

Thank you!

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

    "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]


<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] lang="en" xml:lang="en">

<head>


<title>Validate userID and Password</title>


<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />


<meta http-equiv="Content-Language" content="en-us" />

<meta name="description" content="JavaScript" />
<meta name="keywords" content="JavaScript" />



<!-- the absolute pathname of this file is:            -->

<!-- C:\MISCSUP2_10_17_8\MISCSUP2\Javascript_Demystified_Download_Working\ -->
<!-- Ch_2_Page32_thru_34.html -->

<!-- this file was created on Monday, 08/09/10:                      -->
<!-- this file was updated on Monday, 08/09/10:                      -->


</head>

<body>
      
<script language="Javascript" type="text/javascript">

         <!--   

         var userID 
         var password
         userID = prompt('Enter user ID',' ')
         password = prompt('Enter password',' ')
         if (userID == 'ScubaBob' &amp;&amp; password == 'diving')
         {
            alert('Logon valid')
         }
         else
         {
            alert('Logon invalid')
         }

         -->
         
      </script>

</body>

</html>
 
I'm not sure whether your original source code contained "&amp;&amp;", or whether this site has done some crazy replacement on your code, but that's the only problem I see. If I change "&amp;&amp;" to "&&", it works fine in both Fx and IE 8.

Hope this helps,

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Hello!

I figured it out! My original code did contain the two ampersands, and this site did some crazy replacement.

What happened is that when I load the file into the Browser, I used the cursor and left clicked into the "Enter user ID" prompt box, then typed in the user ID. I then noticed that there is a space to the left of what I typed in. This space was generated by the Browser software (or lack thereof), not by me hitting the space bar. This is what caused the JavaScript file to not work properly.

When I reload the file and do not click into the prompt box, but simply type in the user ID, I notice that there is no space to the left of what was entered, which is how it should be. Then it worked!!!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top