oOple.com Forums

oOple.com Forums (http://www.oople.com/forums/index.php)
-   The PlayGround (http://www.oople.com/forums/forumdisplay.php?f=45)
-   -   Anyone care to help me debug my crappy JavaScript please? (http://www.oople.com/forums/showthread.php?t=23713)

c0sie 09-05-2009 04:28 PM

Anyone care to help me debug my crappy JavaScript please?
 
I have to produce a webpage that uses a while{} to ensure that a valid password is entered into a prompt box. Valid passwords include atleast 6 characters, include a @ and begin with the number 0.

Any ideas?

Thanks in advance!!

Code:

<html>
<head>

<script language = "JavaScript">

//Declare and initilise variables
        var intIncludesAt = 0;
        var strPassword = prompt("Please enter a valid password","");
        var intFirstChar;
        var intValidPassword = 0;
        var strCharCheck;

        function validatePassword()
        {
                while(intValidPassword == 0)
                {       
                        intFirstChar = parseInt(strPassword.charAt(0));
               
                        for (intCount=0; intCount<strPassword.length; intCount++)
                        {
                                if(strPassword.charAt(intCounter) == @)
                                {
                                        intIncludesAt = 1;
                                }
                        }
               
                if (strPassword.length >= 6 && intintFirstChar == 0 && intIncludesAt == 1)
                {
                        intValidPassword = 1;
                }
        }

</script>

</head>

<body>

<input type="button" value="Click here to validate your password" name="cmdValidatePassword" onclick = "validatePassword()">
</body>

</html>


c0sie 09-05-2009 06:25 PM

So anyways...

This is where im at now:

Code:

<script language = "JavaScript">

//Declare and initilise variables
        var intIncludesAt = 0; //Changes to 1 if an @ sign is found
        var intFirstChar; //First character of the password
        var intValidPassword = 0;
        var intIncludesAt = 0;

        function validatePassword()
        {
                var strPassword = prompt("Please enter a valid password","");
               
                while(intValidPassword == 0)
                {       
                        intFirstChar = parseInt(strPassword.charAt(0));
               
                        for (intCount = 0; intCount < strPassword.length; intCount ++)
                        {
                                if(strPassword.charAt(intCount) == "@")
                                {
                                        intIncludesAt = 1;
                                }
                        }
               
                        if (strPassword.length >= 6 && intIncludesAt == 1 && intFirstChar == 0)
                        {
                                intValidPassword = 1;
                        }
                }                       
               
                if (intValidPassword = 1);
                        alert("Your password is valid!")
        }
</script>

The problem im getting now is that if I enter any password at all it says that the password is valid...but if I enter nothing and press the button it slows IE8 and shows an error relating to IE8 :/

markwilliamson2001 09-05-2009 08:33 PM

I think that this is proof that IE8 is "not ready yet"...typical of any microsoft product when it is actually released! Have you tried it with IE7/Firefox 3/Safari etc etc? Does it work in these?

Worth a shot: If you want a quick way of trying out IE7, you need to look at Virtual PC 2007 from MS. Its a free download, and will let you run a seperate XP OS at the same time as your host OS. Might be worth getting hold of some extra RAM which you can assign to the Virtual OS, as it will slow your machine down a bit.
HTH
Mark

c0sie 10-05-2009 02:15 PM

Cheers for that Mark! Sounds like you've been in my shoes before then? What area do you work in then?

c0sie 10-05-2009 02:50 PM

Right, just need one piece of advice if anyone is able to help??

When I run the above code, and enter a VALID password it shows the alert as below, however, if the password is NOT valid the code just seems to get lost and then I have to close IE.

Does anyone know how I can get past
Code:

if (intValidPassword = 1);
                        alert("Your password is valid!")

and then back to the top if the password is invalid?

Thanks :)

Code:

<script language = "JavaScript">


        function validatePassword()
        {
                //Declare and initilise variables
                var intIncludesAt = 0; //Changes to 1 if an @ sign is found
                var intValidPassword = 0;
                var intIncludesAt = 0;
                var strPassword = prompt("Please enter a valid password","");
                var intFirstChar = parseInt(strPassword.charAt(0)); //First character of the password
               
                while(intValidPassword == 0)
                {       
                        for (intCount = 0; intCount < strPassword.length; intCount ++)
                        {
                                if(strPassword.charAt(intCount) == "@")
                                {
                                        intIncludesAt = 1;
                                }
                        }
               
                        if (strPassword.length >= 6 && intIncludesAt == 1 && intFirstChar == 0)
                        {
                                intValidPassword = 1;
                        }
                }                       
               
                if (intValidPassword = 1);
                        alert("Your password is valid!")
        }
</script>


Hairy Spider 10-05-2009 03:49 PM

Under 'fail' conditions, your while loop is not getting any new information upon which to re-evaluate the situation. Once a fail, always a fail :( and that is not IE8's fault. Moving 1 line will cure the problem.

Regards,
Jon.

c0sie 10-05-2009 04:00 PM

Jon,
Can you give me a hint as to which line to move?

Thanks for the reply :)

Cris

c0sie 10-05-2009 04:02 PM

Also, I presume that
Code:

if (intValidPassword = 1);
                        alert("Your password is valid!")

should be

Code:

if (intValidPassword == 1);
                        alert("Your password is valid!")

??

Thanks :)

Hairy Spider 10-05-2009 04:46 PM

Actually it's 2 lines. I was looking at post 2 when I wrote 1 line.

Moving
Quote:

strPassword = prompt("Please enter a valid password","");
into the while loop will give it a new password to check every loop

Tweaked a little further it will handle the cancel button too:

Quote:

<script language = "JavaScript">

function validatePassword()
{
//Declare and initilise variables
var intIncludesAt = 0; //Changes to 1 if an @ sign is found
var intValidPassword = 0;
var intIncludesAt = 0;
var strPassword = "";
var intFirstChar = 0;
var abort = 0;

while(intValidPassword == 0 && abort == 0)
{
strPassword = prompt("Please enter a valid password","");

if(strPassword != null) // Will be null if cancel button pressed
{
intFirstChar = parseInt(strPassword.charAt(0)); //First character of the password
for (intCount = 0; intCount < strPassword.length; intCount ++)
{
if(strPassword.charAt(intCount) == "@")
{
intIncludesAt = 1;
}
}

if (strPassword.length >= 6 && intIncludesAt == 1 && intFirstChar == 0)
{
intValidPassword = 1;
alert("Your password is valid!")
}
}
else
{
abort = 1;
alert("Password entry aborted")
}
}

}
</script>
(For some reason all the indentation has gone :eh?:)

And yes to your last question.

Regards,
Jon.

c0sie 10-05-2009 04:50 PM

*bows at your feet*

Thanks a tonne Jon!

Hairy Spider 10-05-2009 06:52 PM

Quote:

Originally Posted by c0sie (Post 239346)
Thanks a tonne Jon!

No Worries,
Jon.


All times are GMT. The time now is 10:49 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
oOple.com