Welcome to MoiK78 blog. Your daily intake of Internet news. This is a personal site so I post whenever I can :) but I'll try to get it daily. Be free to email me any suggestion. El bolg de Moisés García sobre tecnologías de internet y programas de diseño.
Homepage About Me Contact Me Buy at MoiK78 online shop Downloads

Welcome to MoiK78 blog, your daily intake of internet and technology news.

Picasa Web Albums My Picasa Photo Albums / Albumes de Fotos

November 07, 2002

Today new methods for String object.
Well, today I'm going to post some new methods to check if the string is a number, alphanumeric or text without numbers. We are going to need one method from yesterday: String.prototype.SpanishLetter. Check this post to refresh it.
Now we are going to check if it's a number:
String.prototype.Num = function() {
var num = this.toString();
if ((num >= "0") && (num <= "9")) {
return true;
} else {
return false;
}
}

Usage:
myString = new String("9");
if (myString.Num()) {
trace("Number");
} else {
trace("Not Number");
}


Now we are going to check if the string is numeric:
String.prototype.Numeric = function(){
var temp= this.toString();
for (var i=0; i < temp.length; i++) {
var character= temp.charAt(i)
if (!character.Num()) {
return false;
}
}
return true;
}

Usage:
myString = new String("005146");
if (myString.Numeric()) {
trace("Numeric string");
} else {
trace("Not Numeric string");
}


Now we are going to check if our string is a text.
String.prototype.Alphabetic = function(){
var temp = this.toString();
for (var i=0; i < temp.length; i++) {
var character = temp.charAt(i)
if ( !character.SpanishLetter() ) {
return false;
}
}
return true;
}

Usage:
myString = new String("this is a can");
if (myString.Alphabetic()) {
trace("Alphabetic string");
} else {
trace("Not Alphabetic string");
}


And the last one we are going to check if the string is alphanumeric, so it contains numbers and letters.
String.prototype.Alphanumeric = function(){
var temp= this.toString();
for (var i=0; i < temp.length; i++) {
var character = temp.charAt(i)
if ( !(character.SpanishLetter() || character.Num())) {
return false;
}
}
return true;
}

Usage:
myString = new String("My telephone is 12345");
if (myString.Alphanumeric()) {
trace("It contains letters and numbers");
} else {
trace("Not containing letters and numbers");
}


 
Site Meter