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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validation help 1

Status
Not open for further replies.

lexx2gee

Programmer
Mar 1, 2005
56
GB
Hi all,

I am using a simple script to validate a field in a form to ensure that the correct password is entered into the field before the person can get to the next page.

Currently it only take one password but I want to add another yet am struggling using an or condition in the javascript. Here is original code

--------------

function validate_form ( )
{
valid = true;

if ( document.getElementById('passbox_name').value == "" )
{
alert ( "Please enter your password" );
valid = false;
}

else if (document.getElementById('passbox_name').value != "aut2007" )
{
alert ( "Incorrect password, try again" );
valid = false;
}


if(valid)
{
document.location.href="key-availability-holidays.aspx?type=3&e=1844";
}
return valid;
}

-------------------------

So if someone enters aut2007 they are passed to another page but how do i say they could also enter aut2008 to go to the same page?

Thanks a lot

Lexx
 
I know this doesn't answer your question, but don't you think that having the password cleartext in the source of the page defeats the point of having a password altogether?

Anyone can simply view the source, and bingo - they're in!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Hi

Hopefully you are only playing around with otherwise publicly available stuff, knowing that JavaScript based authentication means zero security.
Code:
else if (document.getElementById('passbox_name').value != "aut2007" [red]&& document.getElementById('passbox_name').value != "aut2008"[/red])

[gray]# or[/gray]

else if ([red]![/red]document.getElementById('passbox_name').value[red].match(/^aut200[78]$/)[/red])

Feherke.
 
I'll show you what's going on, but you really should NEVER validate passwords through Javascript, unless this is a low risk. A person could disable javascript and get into you site w/o ever entering a password.

Passwords should be validated server side.

As to your code, take a look at this:

Code:
function validate_form ( )
{
   if ( document.getElementById('passbox_name').value == "" ) {
        alert ( "Please enter your password" );
        return false;
    }
   if (document.getElementById('passbox_name').value == "aut2007" || document.getElementById('passbox_name').value == "aut2008") {
      document.location.href="key-availability-holidays.aspx?type=3&e=1844";    
   }
   else {
      alert("Incorrect password, try again."); 
      return false;    
   }
}


[monkey][snake] <.
 
Thanks for the help,

dont worry about the security this is just to be used for fun it does not give access to anything that needs to be kept secure otherwise I would use a database with server side coding.

Works great though, I was nearly there with the || but was using the wrong syntax below.

Cheers

Lexx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top