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;
}
本文介绍了一种在JavaScript中扩展String.prototype的方法,通过定义新的email方法来检查字符串是否符合电子邮件地址的标准格式。此外,还展示了如何在实际应用中利用这个自定义方法进行验证。
152

被折叠的 条评论
为什么被折叠?



