Jump to content



Photo

logon function


  • Please log in to reply
17 replies to this topic

#1 damindor

damindor

    Neowinian

  • Joined: 30-December 11

Posted 11 February 2014 - 09:54

I am at my wits end why this wont work any help would be appreciated

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<head>
<title>Untitled Document</title>
<script>
function logon()
{var userid
 var password
 var valid
 userid=prompt('enter userid','')
 password=prompt('enter password','')
 valid=validatelogon(userid,password)
 if(valid==true)
 {
 alert('valid logon')
 }
 else
 {
 alert('invalid logon')
 
 }
 function validatelogon(id,pwd)
 {
 var Returnvalue
 if(id=='peter' && pwd =='tree')
 {
 Returnvalue=true
 }
 else
 {
 Returnvalue=false
 }
 return Returnvalue
 
 
 }
}
</script>
</head>
 
<body>
Logon()
 
</body>
</html>
 



#2 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 15
  • Joined: 23-January 11
  • Location: Vlissingen, Netherlands
  • OS: Windows 8 Pro
  • Phone: Sony Xperia Z White

Posted 11 February 2014 - 09:57

Use ;

#3 +riahc3

riahc3

    Neowin's most indecisive member

  • Tech Issues Solved: 11
  • Joined: 09-April 03
  • Location: Spain
  • OS: Windows 7
  • Phone: HTC Desire Z

Posted 11 February 2014 - 09:59

Hello,

 

As a personal suggestion always state the type of script:

 

<script type="text/javascript>

 

Im not sure if thats standard compliant but it makes the code a bit easier.

 

Im gonna try out your code...



Use ;

I looked at script type not being stated and didnt even look that it had a bunch of ";" missings :laugh:

#4 the better twin

the better twin

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 26-January 10
  • OS: Win 8
  • Phone: Nokia Lumia

Posted 11 February 2014 - 10:20

 

Hello,

 

As a personal suggestion always state the type of script:

<script type="text/javascript>

Im not sure if thats standard compliant but it makes the code a bit easier.

 

Im gonna try out your code...



I looked at script type not being stated and didnt even look that it had a bunch of ";" missings :laugh:

 

As of html 5 a script declaration is unnecessary. For some reason, the op is using xhtml which should use it for standards but should still work. 

 

Whats wrong here:

Case sensitivity: you name your function logon, and then call the function Logon that doesnt exist.

Function outside script tags. Why are you just calling "logon" in the body? You need to wrap all javascript in script tags (apart from some cases like forms).

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<script>
function logon() {
	var userid;
 	var password;
 	var valid
 	
 	userid=prompt('enter userid','');
 	password=prompt('enter password','');
 	valid=validatelogon(userid,password);
 
 	if(valid==true) {
 		alert('valid logon');
 	} else {
 		alert('invalid logon');
 	}
 
function validatelogon(id,pwd) {
 	var Returnvalue;
 	if(id=='peter' && pwd =='tree') {
 		Returnvalue=true;
 	} else {
 		Returnvalue=false;
 	}
 return Returnvalue;
}

}
</script>
</head>
 
<body>
<script>
logon();
</script>
 
</body>
</html>
 


#5 +riahc3

riahc3

    Neowin's most indecisive member

  • Tech Issues Solved: 11
  • Joined: 09-April 03
  • Location: Spain
  • OS: Windows 7
  • Phone: HTC Desire Z

Posted 11 February 2014 - 10:56

Hello,

As of html 5 a script declaration is unnecessary.

Ah OK, thats why I wasnt sure if it was standard or optional now. Thank you.
 


Whats wrong here:
Case sensitivity: you name your function logon, and then call the function Logon that doesnt exist.
Function outside script tags. Why are you just calling "logon" in the body? You need to wrap all javascript in script tags (apart from some cases like forms).

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<script>
function logon() {
	var userid;
 	var password;
 	var valid
 	
 	userid=prompt('enter userid','');
 	password=prompt('enter password','');
 	valid=validatelogon(userid,password);
 
 	if(valid==true) {
 		alert('valid logon');
 	} else {
 		alert('invalid logon');
 	}
 
function validatelogon(id,pwd) {
 	var Returnvalue;
 	if(id=='peter' && pwd =='tree') {
 		Returnvalue=true;
 	} else {
 		Returnvalue=false;
 	}
 return Returnvalue;
}

}
</script>
</head>
 
<body>
<script>
logon();
</script>
 
</body>
</html>
 

Careful, I got almost warned for posting the complete code solution to a problem and my code was removed :(

I tried his code on my PC and everything you said and Seahorsepip is basically what is missing/wrong.

#6 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 15
  • Joined: 23-January 11
  • Location: Vlissingen, Netherlands
  • OS: Windows 8 Pro
  • Phone: Sony Xperia Z White

Posted 11 February 2014 - 15:25

Oh and just some stuff everybody keeps doing wrong....
var auto = 0;
if(auto){
    alert("yes");
}
if(!auto){
    alert("no");
}
 
is simpler code for the commonly used:
 
if(auto==true){
    alert("yes");
}
if(auto==false){
    alert("no");
}
 
Keep in mind true and false aren't the only values that work:
 
In above code the following values return true:
auto = true;
auto = 1;
auto = "Any sort of text";
auto = some number;
 
In above code the following values return false:
auto = false;
auto = 0;
auto = "";


auto = null ;//Can be used to remove a value from a veriable
auto = undefined; //An error you sometimes get when you forget to set a value
auto = NaN; //Same message you sometimes get when a functions returns nonthing
 
I recommend to use 1 and 0 since it's less bothersome to type and shorter :p
 
Though you can use all of them, except the last 3 values for false, those should be used when you actually know more about them and when they should be used.
 
Oh and never forget to put ; behind a function or setting a variable, examples:
 
heythere();
auto = 0;
 
And know the difference between the following 2 things:
var auto = 0;
auto = 0;
 
In the first case you CREATE a new variable and in the second case you MODIFY a already existing variable.
 
And variables can be set inside functions and outside them as example for settings them inside:
 
function count(){
    var auto = 5;
    if(auto==5){
        alert("Auto is 5");
    }
}
function car(){
    var auto = 5;
    if(auto<6){
        alert("You got less then 6 cars, to be exact you got "+auto+" cars.");
    }
}
 
The above code sets the variable auto twice which is unnecessary, instead write the variable above the functions:
 
var auto = 5;
function count(){
    if(auto==5){
        alert("Auto is 5");
    }
}
function car(){
    if(auto<6){
        alert("You got less then 6 cars, to be exact you got "+auto+" cars.");
    }
}
 
Then you can use 'auto = 4;' to change the global variable auto inside the functions, now you also don't have to use 'var' anymore since you're modifying a already existing value.
 
Also this prevents unnecessary passing of variables trough functions since all functions can access this variable now.
 
Also know when to use "" and when not, when you're working with the booleans(false or true) you don't need to use them and also when working with numbers but when you're working with text never forget to use them.
 
Some tricky things that clear up the importance of using "" when necessary or when not:
 
auto = "5";
car = 3;


verhicles = auto+car; //in this case verhicles will give "53"...


pink = 5;
red = 3;


colors = pink+red; //in this case colors will give 8
 
Also when writing comments inside code you can use:
 
// for 1 line comments
 
and
 
/*
Multiple
lines
of
comments
*/
 
I guess this is enough for today's lessons :p


#7 +riahc3

riahc3

    Neowin's most indecisive member

  • Tech Issues Solved: 11
  • Joined: 09-April 03
  • Location: Spain
  • OS: Windows 7
  • Phone: HTC Desire Z

Posted 11 February 2014 - 15:29

Hello,

Oh and just some stuff everybody keeps doing wrong....

var auto = 0;
if(auto){
    alert("yes");
}
if(!auto){
    alert("no");
}

is better code for the commonly used:

if(auto==true){
    alert("yes");
}
if(auto==false){
    alert("no");
}

I, for legacy reasons, disagree.

I personally:

if(auto==true){
    alert("yes");
}
else if(auto==false){
    alert("no");
}
else{
    alert("something else");
}
Yup, its more code and Im sure your method is more efficient but I guess Im old school for some things :p

#8 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 15
  • Joined: 23-January 11
  • Location: Vlissingen, Netherlands
  • OS: Windows 8 Pro
  • Phone: Sony Xperia Z White

Posted 11 February 2014 - 15:35

Hello,
I, for legacy reasons, disagree.

I personally:
 

if(auto==true){
    alert("yes");
}
else if(auto==false){
    alert("no");
}
else{
    alert("something else");
}
Yup, its more code and Im sure your method is more efficient but I guess Im old school for some things :p

 

Haha yeah I also used to write true and false everytime but It's just so bothersome to keep writing that xD

 

Oh and a last thing people tend to forget or don't even know:

auto = 5;
color = 5;
red = 5;
blue = 5;
windows = 5;

can also be written as:

auto = color = red = blue = windows = 5;

This way you can set multiple variables at once easier ^^



#9 +riahc3

riahc3

    Neowin's most indecisive member

  • Tech Issues Solved: 11
  • Joined: 09-April 03
  • Location: Spain
  • OS: Windows 7
  • Phone: HTC Desire Z

Posted 12 February 2014 - 08:47

Hello,

Haha yeah I also used to write true and false everytime but It's just so bothersome to keep writing that xD

Yeah, thats understandable :) I like code that is explicit, but thats a personal thing and your code is better.
 

Oh and a last thing people tend to forget or don't even know:

auto = 5;
color = 5;
red = 5;
blue = 5;
windows = 5;
can also be written as:
auto = color = red = blue = windows = 5;
This way you can set multiple variables at once easier ^^

 

Yup; This "feature" is mostly supported by scripting languages (some high level programming languages might support it to, IDR)



#10 ShockD

ShockD

    Antagonist

  • Joined: 15-October 13
  • Location: 127.0.0.1

Posted 12 February 2014 - 11:28

Hello,

Yeah, thats understandable :) I like code that is explicit, but thats a personal thing and your code is better.
 

Yup; This "feature" is mostly supported by scripting languages (some high level programming languages might support it to, IDR)

What feature? Isn't that like just a logical expression that can be used by any high level language?



#11 +riahc3

riahc3

    Neowin's most indecisive member

  • Tech Issues Solved: 11
  • Joined: 09-April 03
  • Location: Spain
  • OS: Windows 7
  • Phone: HTC Desire Z

Posted 12 February 2014 - 11:36

Hello,

What feature? Isn't that like just a logical expression that can be used by any high level language?

Nope. It doesnt work in every single high level language.

#12 ShockD

ShockD

    Antagonist

  • Joined: 15-October 13
  • Location: 127.0.0.1

Posted 12 February 2014 - 11:45

Hello,
Nope. It doesnt work in every single high level language.

It works in C so other high level languages should be ashamed of themselves...



#13 +riahc3

riahc3

    Neowin's most indecisive member

  • Tech Issues Solved: 11
  • Joined: 09-April 03
  • Location: Spain
  • OS: Windows 7
  • Phone: HTC Desire Z

Posted 12 February 2014 - 13:55

Hello,

It works in C so other high level languages should be ashamed of themselves...

Personally, its a feature I dont use much but Reading around not all languages have it. Most do :)

#14 +Seahorsepip

Seahorsepip

    http://seapip.com

  • Tech Issues Solved: 15
  • Joined: 23-January 11
  • Location: Vlissingen, Netherlands
  • OS: Windows 8 Pro
  • Phone: Sony Xperia Z White

Posted 12 February 2014 - 14:18

It works in C so other high level languages should be ashamed of themselves...

 

Depends, some languages don't support it but support a other syntax instead:

auto, color, verhicle, motor = 5;

Also some languages have int and String instead of var and other types of syntax like $variable, so for each language you start working with you have to look again into the syntax  ^^



#15 ShockD

ShockD

    Antagonist

  • Joined: 15-October 13
  • Location: 127.0.0.1

Posted 12 February 2014 - 16:16

Depends, some languages don't support it but support a other syntax instead:

auto, color, verhicle, motor = 5;

Also some languages have int and String instead of var and other types of syntax like $variable, so for each language you start working with you have to look again into the syntax  ^^

Yes, I believe everybody here is aware of that.





Click here to login or here to register to remove this ad, it's free!