www.webdeveloper.com
Results 1 to 9 of 9

Thread: What's wrong with the following code?

  1. #1
    Join Date
    Oct 2003
    Posts
    8

    What's wrong with the following code?

    Can someone tell me what's wrong with the following code? It's suppose to show the telephone number only if it consists out of numbers, but if you enter 123e456 it displays it even though it is not a valid number because it contains an alphabet character.

    <!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html>
    <head>

    <title>Telephone Number</title>

    <script langauge="Javascript">

    var tel

    </script>

    </head>

    <body>

    <script language="JavaScript">

    var tel= prompt("Please enter your telephone number:", "")
    if(isNaN(tel))
    {
    document.writeln("You must enter only numbers.")
    } if(tel.length<7)
    {
    document.writeln("You did not enter a telephone number.")
    } if(tel.length>7)
    {
    document.writeln("You did not enter a telephone number.")
    } else {
    document.writeln("You entered:" +tel)
    }

    </script>


    </body>

    </html>

  2. #2
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    You could just add this to remove any non-numeric characters.

    tel=parseInt(tel.replace(/\D/g,""));
    Every fight is a food fight when you’re a cannibal.

  3. #3
    Join Date
    Oct 2003
    Posts
    8
    Where must I add it? Which part of the code? I'm new to JavaScript and I'm not familiar with this function.

  4. #4
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    I modified it to make it slightly more efficient, I'll explain how it works if you want.

    <script type="text/javascript"><!--

    var tel=prompt("Please enter your telephone number:", "").replace(/\D/g,"");

    while(tel.length!=7){

    tel=prompt("Please enter your telephone number:", "").replace(/\D/g,"");

    }

    document.write("You entered:" +tel);

    //--></script>
    Last edited by David Harrison; 10-18-2003 at 09:46 AM.
    Every fight is a food fight when you’re a cannibal.

  5. #5
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    Ignore the last script, there was a bug in it. It didn't take account of the cancel button being pressed, this one does:

    <script type="text/javascript"><!--

    var tel="";


    while(tel.length!=7){

    tel=prompt("Please enter your telephone number:", "");
    tel=(!tel)?"":tel.replace(/\D/g,"");

    }


    document.write("You entered:" +tel);

    //--></script>
    Last edited by David Harrison; 10-18-2003 at 10:26 AM.
    Every fight is a food fight when you’re a cannibal.

  6. #6
    Join Date
    Oct 2003
    Posts
    8
    Thanks for all your help.

    Can you please explain this line of code to me.

    tel=(!tel)?"":tel.replace(/\D/g,"");

    Thanks

    Yolandi

  7. #7
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    Well the /\D/g bit is a regular expression. In RegExp's, rather than double quotes, forward slashes are used to contain them, this is to distinguish them from strings.

    The \D bit is the RegExp symbol for a non-number, so the whole RegExp is just a non-number, the g at the end just tells it to search every character in the string that it is applied to. So in this case it will check all characters in the sting to see wherether they are non-numbers.

    .replace("a","b")

    checks the first character to see whether it is an a, if it is it will replace it with a b, so:

    .replace(/\D/g,"b")

    will check every character to see if they are non-numbers, if they are it replaces them with b, in this one:

    .replace(/\D/g,"")

    if it finds a non-number, it removes it but doesn't replace it with anything.

    tel=tel.replace(/\D/g,"");

    Tells JavaScript to remove any non-numbers from the string variable tel and then give this new string value to the variable tel.

    There is sometimes a way to shorten if/else statements and I have used one here:

    tel=(!tel)?"":tel.replace(/\D/g,"");

    The if/else version of that would be this:

    if(!tel){tel="";}
    else{tel=tel.replace(/\D/g,"");}

    This shortend version can only be used for if you are applying a value to a variable. You can also do things with it like:

    documet.write((!tel)?"":tel.replace(/\D/g,""));

    That just saves time, creating the variable, applying a value to it and then calling the variable later on.

    The !tel bit checks to see if tel has a null value, ie: all variables the do not exist have a null value, and so does the variable tel if the cancel button of the prompt box is pressed, this is because no value is applied to the variable.
    The symbol != means not equal to, I have used it here:

    while(tel.length!=7)

    the ! symbol is very useful for reversing meanings, I tend to think of it as "not".

    Well I bet you never thought that such a little bit of code would need so much explaining, I guess that's why I was able to shorten your code so much, because most of it is contained in that one line.
    Last edited by David Harrison; 10-19-2003 at 09:44 AM.
    Every fight is a food fight when you’re a cannibal.

  8. #8
    Join Date
    Oct 2003
    Posts
    8
    Thanks for all your help.

    It's still a bit unfamiliar, but it is much clearer now and the best of all it's working.

    Thanks again. I'll be posting some more questions as I work through my material, so please be on the lookout for my posts. There's not many people out there liking to explain code to beginners. Thanks for your patience.

  9. #9
    Join Date
    Dec 2002
    Location
    Manchester, UK
    Posts
    6,276
    Happy to help.

    Although I seem to be helping less these days, my school work is much harder than it was last year.
    Every fight is a food fight when you’re a cannibal.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles