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 2 of 2
  1. #1
    New Coder
    Join Date
    Mar 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Learning javascript on my own...I really need your help

    I'm very new at this and trying to learn on my own. I'm going through a book I got from a friend who took a javascript class a few years ago and working on some of the example assignments. I'm having trouble (of course) and would love for someone I could email regulary that would be able to help me... or just post on here often with dumb questions

    here's one I'm kinda stuck on right now...

    I think I might have one working that checks to see if the user inputs a number and to check to see if it is odd or even. The only thing is if you enter more than 1 number it'll prompt you more than once? Any ideas?

    <script language="JavaScript"><!--
    function oddEven() {
    var entry=document.textForm.textBox.value;

    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;
    for (i = 0; i < entry.length && IsNumber == true; i++)
    {
    Char = entry.charAt(i);
    if (ValidChars.indexOf(Char) == -1)
    {
    alert ("Please Enter a Numeric Value.")
    }

    if (ValidChars.indexOf(Char) != -1)
    {
    var Mod= (entry % 2) ? "This value is odd!" : "This value is even!";
    alert (Mod);
    }

    }

    }
    //--></script>





    the next thing I want to do it figure out how to change that code into check to see that the user inputs a number that is 9 characters long. Is this it?

    var length= (entry.length <= 9) ? "This value is less than 9!" : "This value is greater than 9!";
    alert (length);

    but again, the # of characters you enter equals the number of prompts you get... what am I doing wrong?


    I would even be willing to pay someone a small few to help kinda tutor me when I need help with some of these examples. Once I see how they are correctly coded it begins to make sense.. just having a hard time at the beginning. PM me if interested...
    Last edited by disneyfan; 03-07-2005 at 01:29 PM.

  • #2
    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
    the regular expression are much suitable to this kinda validator, for instance, to check if the entry is a number or not, u may use this simple
    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <
    html>
    <
    head>
    <
    title>Untitled Document</title>
    <
    meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <
    meta http-equiv="Content-Style-Type" content="text/css">
    <
    meta http-equiv="Content-Script-Type" content="text/javascript">
    <
    script language="JavaScript" type="text/JavaScript">
    function 
    isNumber(field) {
            var 
    re = /^[0-9-'.']*$/;
            if (!
    re.test(field.value)) {
                
    alert('Value must be all number charcters, including "." ,b non numbers will be removed from field!');
                
    field.value field.value.replace(/[^0-9-'.']/g,"");
            }
        }
    </script>
    </head>
    <body>
    <textarea name="text" cols="" rows="" onkeyup="isNumber(this)"></textarea>
    </body>
    </html> 
    To check whether the number is even or odd, you might a similar filter applied uppon the last typed character (usin, split('') method, for instace. If it is 0,2,4,6,8 is even, else it is odd... Or you might divide the transformed value to 2 and check wheter the result is floated or not...

    To check the lenght of the input is even simplier. Just use the length attribute.
    Last edited by Kor; 03-07-2005 at 01:51 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

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