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)
}
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:
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.
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.
Bookmarks