Functions That Return Values Can Be Used in Expressions
/*
JavaScript Unleashed, Third Edition
by Richard Wagner and R. Allen Wyke
ISBN: 067231763X
Publisher Sams CopyRight 2000
*/
<html>
<head>
<title>JavaScript Unleashed</title>
<script type="text/javascript">
<!--
function isPhone(aString) {
var aChar = null;
var status = true;
if(aString.length != 13) {
status = false;
}else{
for(var i = 0; i <= 12; i++) {
aChar = aString.charAt(i);
if ( i == 0 && aChar == "(" ){
continue;
}else{
if( i == 4 && aChar == ")" ){
continue;
}else{
if( i == 8 && aChar == "-" ){
continue;
}else{
if( parseInt(aChar,10) >= 0 && parseInt(aChar,10) <= 9 ){
continue;
}else {
status = false;
break;
}
}
}
}
}
}
return(status);
}
// -->
</script>
</head>
<body>
<script type="text/javascript">
<!--
var userInput = "(800)555-1212";
if(isPhone(userInput)) {
document.writeln("Thank you for your phone number.");
document.writeln("I will have a representative get you");
document.writeln("more information.");
}else{
document.writeln("Please re-enter your phone number");
document.writeln("using the format (###)###-####");
}
//-->
</script>
</body>
</html>
Related examples in the same category