JavaScript中函数调用的一种扩展方法:
check1.js:
String.prototype.email = testEmail;//判断电子邮箱格式,扩展方法
//------------------------------判断电子邮箱格式------------------------------
function testEmail()
{
if(!this.isNull()){
if(this.search(/^([-_A-Za-z0-9\.]+)@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/)!=-1)
{
return true;
}
else
{
alert("电子邮箱格式不正确!");
return false;
}
}
else
{
return true;
}
}
check2.js:
var email = document.getElementById("email");
if(!email.value.email()){ //使用扩展方法
alert("电子邮件格式不正确!");
email.focus();
return false;
}