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");
}
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");
}