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 cañón");
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");
}
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 cañón");
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");
}