Click to See Complete Forum and Search --> : prompts
Greelmo
05-12-2003, 10:04 PM
I know this is going to sound very pathetic, but i'm not at all experienced when it comes to Java. I have been working on it for MAYBE 3 weeks. I have experience in other languages though. Okay, so here is my question...
When initializing a prompt, how can i get it to have more than one text line. For example... if i wanted a password prompt to pop up and one line to call for the name, and the other for the password. Is that possible?
No, it is not possible; you can, however, tell the user to put a space after their username for their password:
var q = prompt("Enter your:\nUsername\nPassword","user pass");
var a = q.split(" ");
alert("Username: "+a[0]+"\nPassword: +"a[1]);
khalidali63
05-12-2003, 10:12 PM
I don't think so,will be happy to see it otherwise..:-)
Greelmo
05-12-2003, 10:19 PM
thanks. okay, so that sparks another question... what if i want the password and user name to be in a form on the actual document and i want a function to check for both of them to be in some sort of data base (array). What code would i use for that?
OK... Let me think for a while. LOL
This works with one minor (?) bug: it runs three times because of the for() loop. Khalid, I don't know how to get rid of this. Can you help me out to understand this, please? :o
<script type="text/javascript" language="javascript">
var pwrdAry = new Array("onjay","prilay","orikay");
var usrAry = new Array("jon","april","kori");
function test(){
var q = prompt("Enter your username and password:","jon onjay");
for(i=0;i<usrAry.length+pwrdAry.length/2;i++)
if(q.split(" ")[0]==usrAry[i] && q.split(" ")[1]==pwrdAry[i]){
alert("Successful login.\nUser: "+q.split(" ")[0]+"\nPassword: "+q.split(" ")[1]);
} else {
alert("Invalid login.\nUser: "+q.split(" ")[0]+"\nPassword: "+q.split(" ")[1]+"\nIs an invalid password. Please reload the page and try again.");
} }
</script>
<input type=button onClick="test()" value="Test">
Greelmo
05-13-2003, 12:04 AM
okay, i want this simple little function that takes the value of one text box and applies it to another to work.. .but i can't get it... here is the source:
<html>
<head>
<title>JavaScript forms and buttons</title>
<SCRIPT LANGUAGE = "JavaScript">
function fncmsg(texty)
{
Ref = "abcdefghijklmnopqrstuvwxyz0123456789"
Result = ""
for (Count = 0, Count < texty.length, Count++)
{
Char = texty.substring(Count, Count +1);
Num = Ref.indexOf(Char);
EncodeChar = Ref.substring(Num, Num+1);
Result += EncodeChar;
}
document.form1.box2.value = Result;
alert(texty);
}
</script>
</head>
<body>
<form name = "form1">
<input name = "box1" type = text>
<input name = "button1" type = "button" value = "SUBMIT" style = "background: red"
onclick = "JavaScript: fncmsg(form.box1.value);"><br>
<input name = "box2" type = text value = "">
</form>
</body>
</html>
khalidali63
05-13-2003, 12:09 AM
without going through all of the code,here is th eobvious problem,
You have used "Char" for a vriable which is probably being confused by JavaScript engine for a character identifier( a reserved keyword).
chage it to anything else...
This is the fixed code:
<html>
<head>
<title>JavaScript forms and buttons</title>
<SCRIPT LANGUAGE = "JavaScript">
function fncmsg(texty) {
Ref = "abcdefghijklmnopqrstuvwxyz0123456789"
Result = ""
for (Count = 0; Count<texty.length; Count++){
Char = texty.substring(Count, Count +1);
Num = Ref.indexOf(Char);
EncodeChar = Ref.substring(Num, Num+1);
Result += EncodeChar;
}
document.form1.box2.value = Result;
alert(texty);
}
</script>
</head>
<body>
<form name = "form1">
<input name = "box1" type = text>
<input name="button1" type="button" value="SUBMIT" style="background: red"
onclick="fncmsg(this.form.box1.value);"><br>
<input name="box2" type=text value = "">
</form>
</body>
</html>
But this is what I would've done:
<html><head>
<script>
function sendTo(box1, box2){
box2.value=box1.value;
}
</script>
</head><body>
<form name="form1" action=""><div>
<input type=text name="box1" value="Hi">
<input type=button value="SUBMIT" onclick="sendTo(this.form.box1.value, this.form.box2.value);"><br>
<input type=text name="box2">
</div></form></body></html>
khalidali63
05-13-2003, 12:10 AM
Jona,it must not be fixed,or if it is,then...hunmmm..cus CHar could confuse js engine...hence making the solution to some extent prone to problems..
I don't think Char is a reserved word. I know there is charAt() and fromCharCode() but those are methods.
khalidali63
05-13-2003, 12:34 AM
Here is a typical list of javascript reserved/keywords...
http://68.145.35.86/skills/javascripts/JavaScriptKeywords.html
You're right Khalid, but it is lower-case and not capitalized like in the script. Remember, JavaScript is case-sensetive. ;)
OK, here, I fixed it, guys (yaay! right? lol).
<script type="text/javascript" language="javascript">
var pwrdAry = new Array("onjay","prilay","orikay");
var usrAry = new Array("jon","april","kori");
function test(){
var q = prompt("Enter your username and password:","jon onjay");
for(i=0;i<usrAry.length+pwrdAry.length/2;i++)
if(q.split(" ")[0]==usrAry[i]&&q.split(" ")[1]==pwrdAry[i]){
str = "Successful login.\nUser: "+q.split(' ')[0]+"\nPassword: "+q.split(' ')[1]; alert(str); return true;
} else {
str = "Invalid login.\nUser: "+q.split(' ')[0]+"\nPassword: "+q.split(' ')[1]+"\nIs an invalid username and/or password. Please try again.";
alert(str); return false;
} }
</script>
<input type=button onClick="test()" value="Test">
khalidali63
05-13-2003, 11:16 AM
Originally posted by Jona
You're right Khalid, but it is lower-case ....
You are right in saying that JavaScript is case sensitive,
my experience though,any of the key words if you capitalize their first letter,I have noticed it could cause problems...so why take chances...for unrpedictable errors while you can avoid them very easily.
:p
Yeah. I always keep away from using similar variable names. I don't normally get confused (unless I hard code it, or it's reaaaaaalllyyyy long), but it's good to avoid giving yourself a headache... :D
Greelmo
05-13-2003, 06:03 PM
you guys both rock the house... thanks 10,000 fold