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 11, 2002

Another two String methods.
The first one checks if we have introduced a valid mail.
String.prototype.ValidEmail = function () {
var email = this.toString();
if (this.isEmpty()) {
return false;
}
var i = 1;
while ((i < email.length) && (email.charAt(i) != "@")) {
i++;
}
if ((i >= email.length) || (email.charAt(i) != "@")) {
return false;
} else {
i += 2;
}
while ((i < email.length) && (email.charAt(i) != ".")) {
i++;
}
if ((i >= email.length-1) || (email.charAt(i) != ".")) {
return false;
} else {
return true;
}
}

Usage:
myString = new String ("mail@mail.com");
if (myString.ValidEmail()){
trace ("Correct mail");
} else {
trace ("Wrong mail");
}


This one checks if you typed a correct telephone number (here in Spain the phone numbers have to start with 9 and moviles with 6). Change the values with your own rules.
String.prototype.ValidPhone = function () {
var phone = this.toString();
if (this.IsEmpty()) {
return false;
}
if (phone.length !=9 || (phone.charAt(0) !=9 && phone.charAt(0) != 6)) {
return false;
}
for (i = 0; i< phone.length; i++) {
var num= phone.charAt(i);
if (num.isNaN()) {
return false;
}
}
return true;
};

Usage:
myString = new String ("910020021");
if (myString.ValidPhone()){
trace ("Correct phone number");
} else {
trace ("Wrong phone number");
}


 
Site Meter