验证email function IsValidEmail(email) dim names, name, i, cIsValidEmail = true names = Split(email, "@") if UBound(names) <> 1 then IsValidEmail = false exit function end if for each name in names if Len(name) <= 0 then IsValidEmail = false exit function end if for i = 1 to Len(name) c = Lcase(Mid(name, i, 1)) if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then IsValidEmail = false exit function end if next if Left(name, 1) = "." or Right(name, 1) = "." then IsValidEmail = false exit function end if next if InStr(names(1), ".") <= 0 then IsValidEmail = false exit function end if i = Len(names(1)) - InStrRev(names(1), ".") if i <> 2 and i <> 3 then IsValidEmail = false exit function end if if InStr(email, "..") > 0 then IsValidEmail = false end function
function checkEmail(obj)
{
if(obj.value.charAt(0)=="." ||obj.value.charAt(0)=="@"||
obj.value.indexOf('@', 0) == -1 ||obj.value.indexOf('.', 0) == -1
||obj.value.lastIndexOf("@") ==obj.value.length-1 ||
obj.value.lastIndexOf(".") ==obj.value.length-1)
{
alert("您输入的Email不正确,请重新输入!");
obj.focus();
obj.select();
return false;
}
return true;
}
问题2:不能全数字,Str = "0123456789"
Str.indexOf(OBJ.value.charAt(i))==-1)
来判断是否输入的是数字……
<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript1.2">
function isEmailAddress (string) {
var addressPattern =
/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
return addressPattern.test(string);
}
function checkEmail (field) {
if (!isEmailAddress(field.value)) {
alert('Please enter correct email address!');
field.focus();
field.select();
}
}
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="aForm">
Your email address:
<INPUT TYPE="text" NAME="email"
ONCHANGE="checkEmail(this);"
>
</FORM>
</BODY>
</HTML>
/**
* 一些常用的javascript函数
*
* 作者:徐祖宁(唠叨)
* e-mail:[email protected]
* 2002-12-27
*
* 为便于使用,均书写成String对象的方法
* 把他保存为.js文件,可方便的扩展字符串对象的功能
*
* 方法名 功 能
* ----------- --------------------------------
* Trim 删除首位空格
* Occurs 统计指定字符出现的次数
* isDigit 检查是否由数字组成
* isAlpha 检查是否由数字字母和下划线组成
* isNumber 检查是否为数
* lenb 返回字节数
* isInChinese 检查是否包含汉字
* isEmail 简单的email检查
* isDate 简单的日期检查,成功返回日期对象
*
*//*** 删除首位空格 ***/
String.prototype.Trim = function() {
return this.replace(/(^\s*)|(\s*$)/g, "");
}/*** 统计指定字符出现的次数 ***/
String.prototype.Occurs = function(ch) {
return this.split(ch).length-1;
}/*** 检查是否由数字组成 ***/
String.prototype.isDigit = function() {
var s = this.Trim();
return (s.replace(/\d/g, "").length == 0);
}/*** 检查是否由数字字母和下划线组成 ***/
String.prototype.isAlpha = function() {
return (this.replace(/\w/g, "").length == 0);
}/*** 检查是否为数 ***/
String.prototype.isNumber = function() {
var s = this.Trim();
return (s.search(/^[+-]?[0-9.]*$/) >= 0);
}
/*** 返回字节数 ***/
String.prototype.lenb = function() {
return this.replace(/[^\x00-\xff]/g,"**").length;
}/*** 检查是否包含汉字 ***/
String.prototype.isInChinese = function() {
return (this.length != this.lenb());
}/*** 简单的email检查 ***/
String.prototype.isEmail = function() {
var strr;
var mail = this;
var re = /(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i;
re.exec(mail);
if(RegExp.$3!="" && RegExp.$3!="." && RegExp.$2!=".")
strr = RegExp.$1+RegExp.$2+RegExp.$3;
else
if(RegExp.$2!="" && RegExp.$2!=".")
strr = RegExp.$1+RegExp.$2;
else
strr = RegExp.$1;
return (strr==mail);
}/*** 简单的日期检查,成功返回日期对象 ***/
String.prototype.isDate = function() {
var p;
var re1 = /(\d{4})[年./-](\d{1,2})[月./-](\d{1,2})[日]?$/;
var re2 = /(\d{1,2})[月./-](\d{1,2})[日./-](\d{2})[年]?$/;
var re3 = /(\d{1,2})[月./-](\d{1,2})[日./-](\d{4})[年]?$/;
if(re1.test(this)) {
p = re1.exec(this);
return new Date(p[1],p[2],p[3]);
}
if(re2.test(this)) {
p = re2.exec(this);
return new Date(p[3],p[1],p[2]);
}
if(re3.test(this)) {
p = re3.exec(this);
return new Date(p[3],p[1],p[2]);
}
return false;
}
function IsValidEmail(email)
dim names, name, i, cIsValidEmail = true
names = Split(email, "@")
if UBound(names) <> 1 then
IsValidEmail = false
exit function
end if
for each name in names
if Len(name) <= 0 then
IsValidEmail = false
exit function
end if
for i = 1 to Len(name)
c = Lcase(Mid(name, i, 1))
if InStr("abcdefghijklmnopqrstuvwxyz_-.", c) <= 0 and not IsNumeric(c) then
IsValidEmail = false
exit function
end if
next
if Left(name, 1) = "." or Right(name, 1) = "." then
IsValidEmail = false
exit function
end if
next
if InStr(names(1), ".") <= 0 then
IsValidEmail = false
exit function
end if
i = Len(names(1)) - InStrRev(names(1), ".")
if i <> 2 and i <> 3 then
IsValidEmail = false
exit function
end if
if InStr(email, "..") > 0 then IsValidEmail = false
end function