Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Skipping if else?

    Hii guys..


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

    Code:
    		<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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,863
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Change else if(n<200) to
    Code:
    else if(n>=100 && n<200)
    to correct the logic
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Ok so i got,
    Code:
    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..

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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/co...isnan-function
    http://lawrence.ecorp.net/inet/sampl...-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()
    Code:
    var n=Number(document.getElementById('myText').value)
    Last edited by Kor; 09-23-2011 at 10:41 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    Lennard (09-23-2011)

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

    Code:
    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,

    Code:
    <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=)

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I don't understand which is your problem. The code works:
    Code:
    <!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:
    Code:
    $(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.
    Last edited by Kor; 09-23-2011 at 11:30 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    Lennard (09-23-2011)

  • #7
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I don't understand, either, why did you write the functions like:
    Code:
    $(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!
    Last edited by Kor; 09-23-2011 at 11:30 AM.


  •  

    LinkBacks (?)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •