PDA

View Full Version : prompt with multiple inputs - headache



animatix
02-18-2008, 05:40 PM
hi gang.

im trying to execute a prompt in javascript that contains 2 input fields.

1. is login
2. is password

sounds easy enough right?

well i've been researching this all day and all i find is that you can only check one prompt input at a time.

additonally if you want 2 fields to check - it seems that people create their own prompt FORM to do it.

what i want to know is if there is a way for the browser API to let me have 2 fields for input rather than having to create a page for it.

if anyone has any advice on this it'd be greatly appreciated.

Please bear in mind i want the browser to generate the popup verification with TWO fields (like a prompt with 2 fields SIMULTANEOUSLY) not 1 after the other (as is seen here) and i DO NOT want to have to use a second (loaded in) webpage to do it. It really needs to be done by the browser.

my source looks like this right now:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>TBG Portal</title>

<script language="JavaScript">
<!--hide


var password;
var login;


<!-- case sensitive buffers --!>
var log1="demo";
var log2="DEMO";
var log3="Demo";

var pass1="password";
var pass2="PASSWORD";
var pass3="Password";

password=prompt('Password',' ');
login = prompt ('login:', ' ');

if ((password==pass1 || password==pass2 || password==pass3) && (login==log1 || login==log2 || login==log3))
alert('Password and Login Accepted: Click OK to enter'); <!-- this allows page to continue loading--!>
else
{
alert('Password or Login Incorrect: Click OK to try again');
window.location="entry.html"; <!-- this reloads page lockout --!>
}

//-->
</script>
</head>

<body>
VIEWING THIS PAGE. LOCKED OUT TIL PASS LOG CORRECT.
</body>
</html>

boogyman
02-18-2008, 06:46 PM
I hope this page isn't supposed to be secure? your javascript would be viewable to anyone, thus knowing the correct username / password to access the site. also you shouldn't be using XHTML because it is not widely supported, most notably Internet Explorer, which regretfully is still the most popular browser platform. Look into the HTML 4.01 Strict DOCTYPE.

you also have 3 "usernames" and 3 "passwords" in different cases. If you do not care what the case is, you should be using a regular expression, rather than checking each value like you are. PaSSworD is another "valid" case-insensitive value as you are writing it.

now as for your question about a double "prompt" you would need to create a custom module that would mimic the actions of a javascript prompt. this isnt the best script, you can check out a customizable prompt script at http://javascript.internet.com/text-effects/customizable-javascript-prompt.html

Master_script_maker
02-18-2008, 07:29 PM
here is the regexp for you so it is case insensitive. also i would look into a modal (http://www.dynamicdrive.com/dynamicindex8/dhtmlwindow/dhtmlmodal.htm) instead of a prompt :

pass=/^password$/i;
log=/^demo$/i;
and just use:

if(password.match(pass)!=null && login.match(log)!=null)

boogyman
02-18-2008, 07:34 PM
here is the regexp for you so it is case insensitive. also i would look into a modal (http://www.dynamicdrive.com/dynamicindex8/dhtmlwindow/dhtmlmodal.htm) instead of a prompt :

pass=/^password$/i;
log=/^demo$/i;
and just use:

if(password.match(pass)!=null && login.match(log)!=null)

Yes i would look into the modal... that is what the link was to.

Its good coding practice to encapsulate (wrap) your element values in quotation marks, and dont forget to declare them as well. Again not necessary in javascript because it is a "very" loosely typed language, but its good general coding practice



var pass = "/^password$/i";
var log = "/^demo$/i";

Twey
02-18-2008, 07:46 PM
here is the regexp for you so it is case insensitive.Regexes are expensive -- don't use them when there's a better solution available (like converting the value to lower-case before checking [with .toLowerCase()], in this case).
Its good coding practice to encapsulate (wrap) your element values in quotation marks, and dont forget to declare them as well. Again not necessary in javascript because it is a "very" loosely typed language, but its good general coding practiceYou're wrong -- this is a regular expression literal syntax. Any value between two slashes, with optional alphabetic switches at the end, is a regular expression literal. /^password$/i is (more or less) equivalent to (new RegExp("^password$", "i")). Using var to make sure they're treated as variables is a good idea, though.

boogyman
02-18-2008, 07:54 PM
Regexes are expensive -- don't use them when there's a better solution available (like converting the value to lower-case before checking [with .toLowerCase()], in this case).

and by expensive you mean time consuming? I do agree that you should avoid using regular expressions whenever possible.



You're wrong -- this is a regular expression literal syntax. Any value between two slashes, with optional alphabetic switches at the end, is a regular expression literal. /^password$/i is (more or less) equivalent to (new RegExp("^password$", "i")). Using var to make sure they're treated as variables is a good idea, though.


wont that be treated as


password.match(/^password$/i);

Twey
02-18-2008, 08:47 PM
and by expensive you mean time consuming?Very much so, for some expressions.
wont that be treated asEh?