...

View Full Version : Skipping if else?



Lennard
09-23-2011, 10:27 AM
Hii guys..


Im pretty new to Javascript and cant get this working..



<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
</head>
<body>


<script type="text/javascript">

function checkIt(evt) {
evt = (evt) ? evt : window.event
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
status = "This field accepts numbers only."
return false
}
status = ""
return true
}


function notEmpty(){
var n = document.getElementById('myText').value
if (n<100)
test1();
else if(n<200)
test2();
else
{alert("You did not enter a number!")}
}

return:false

$(function test1() {
{alert("1")}
});
$(function test2() {
{alert("2")}
});

</script>
<INPUT TYPE="text" NAME="numeric" onKeyPress="return checkIt(event)" id="myText">
<input type='button' onclick='notEmpty()' value='Form Checker' />

</body>



Hes running function test1 and function test2 straight without looking at the if else statment, what im doing wrong?

Anyway thanks for helping me

Greetz Lennard

abduraooft
09-23-2011, 10:54 AM
Change else if(n<200) to

else if(n>=100 && n<200) to correct the logic

Lennard
09-23-2011, 11:02 AM
Ok so i got,


if (n<100)
test1();
else if(n>=100 && n<200)
test2();
else
{alert("You did not enter a number!")}
}


But still its skipping the if else, and going straight to the functions test1 and test 2




*edit*

I think its something with the var statments, but cant find the answer atm..

Kor
09-23-2011, 11:39 AM
It depends on which values takes the variable n.

You have two different problems there, and you have not chosen the best solution for neither.

1. Validation for numbers
- don't use onkeypress. It is not consistent among the browsers. I would have rather used onkeyup. And, of course, I would have added onblur (what about if the user uses his right mouse to copy/paste the value?)
- validation using the keystrokes detection is rather intricate and no reliable. You could have tested either with isNaN() method, or you might have used a Regular Expression test:

http://www.diveintojavascript.com/core-javascript-reference/the-isnan-function
http://lawrence.ecorp.net/inet/samples/regexp-validate2.php

2. Like almost all the properties of the elements, the property value returns a String primitive, not a Number primitive. That means two primitives like '100' and 100 are equal by value, but not by type. Therefor, some math operations will fail between strings and numbers. The safest way to prevent any possible mismatch is to transform the strings into numbers before anything else
There are several methods. I prefer the Number()


var n=Number(document.getElementById('myText').value)

Lennard
09-23-2011, 11:55 AM
First off al thanks for the help im getting already, doing mine best to understand (only started Javascript last week)

Mine question im not getting after all, is it possible to make a if else statment like


if (n<100)
test1();
else if(n>=100 && n<200)
test2();
else
{alert("You did not enter a number!")}
}

and let it go to the function test1 and test2, since its doing those 2 functions straight when im refreshing mine page.
and when im entering mine input file like 103 its doing nothing, higher then 200 im getting You did not enter a number!.

if deleted the part with the number checker atm,


<script type="text/javascript">


function notEmpty(){
var n=Number(document.getElementById('myText').value)


if (n<100)
test1();
else if(n>=100 && n<200)
test2();
else
{alert("You did not enter a number!")}
}

$(function test1() {
{alert("1")}
});
$(function test2() {
{alert("2")}
});

</script>
<INPUT TYPE="text" NAME="numeric" id="myText">
<input type='button' onclick='notEmpty()' value='Form Checker' />


- don't use onkeypress. It is not consistent among the browsers. I would have rather used onkeyup. And, of course, I would have added onblur (what about if the user uses his right mouse to copy/paste the value?)
Thanks for that tip=)

Kor
09-23-2011, 12:12 PM
I don't understand which is your problem. The code works:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function nonEmpty(){
var n=Number(document.getElementById('myText').value)
if(isNaN(n)){
alert('You did not enter a number!');
}
else{
if(n<100){
test1();
}
else if(n>=100&&n<200){
test2();
}
}
}
function test1(){
alert(1);
}
function test2(){
alert(2)
}
</script>
</head>
<body>
<form action="">
<input type="text" id="myText">
<input type="button" onclick="nonEmpty()" value="Form Checker">
</form>
</body>
</html>

I don't understand, either, why did you write the functions like:


$(function test1() {
{alert("1")}
});
$(function test2() {
{alert("2")}
});


That could be a sort of JQuery syntax, but I don't sense why would you need to write a function in JQuery way. Nor do I understand why do you use JQuery, after all, for such a simple piece of code.

Lennard
09-23-2011, 12:23 PM
I don't understand, either, why did you write the functions like:


$(function test1() {
{alert("1")}
});
$(function test2() {
{alert("2")}
});


That could be a sort of JQuery syntax, but I don't sense why would you need to write a function in JQuery way. Nor do I understand why do you use JQuery, after all, for such a simple piece of code.

THANKS! it was those brackets, this is just the begin of something totally different (made it easy for me to understand first)

But this was the solution!:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum