/    Sign up×
Community /Pin to ProfileBookmark

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>

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonOct 17.2003 — You could just add this to remove any non-numeric characters.

tel=parseInt(tel.replace(/D/g,""));
Copy linkTweet thisAlerts:
@NetSurferauthorOct 18.2003 — Where must I add it? Which part of the code? I'm new to JavaScript and I'm not familiar with this function.
Copy linkTweet thisAlerts:
@David_HarrisonOct 18.2003 — 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>
Copy linkTweet thisAlerts:
@David_HarrisonOct 18.2003 — 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>
Copy linkTweet thisAlerts:
@NetSurferauthorOct 19.2003 — Thanks for all your help.

Can you please explain this line of code to me.

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

Thanks

Yolandi
Copy linkTweet thisAlerts:
@David_HarrisonOct 19.2003 — 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.
Copy linkTweet thisAlerts:
@NetSurferauthorOct 19.2003 — 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.
Copy linkTweet thisAlerts:
@David_HarrisonOct 19.2003 — Happy to help. ?

Although I seem to be helping less these days, my school work is much harder than it was last year.
×

Success!

Help @NetSurfer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ,
analytics: Fullres
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Marika,
tipped: article
amount: 1000 SATS,

tipper: @hatem774,
tipped: article
amount: 1 SATS,

tipper: @nearjob,
tipped: article
amount: 1000 SATS,
)...