ClientID=Request.Form("ClientID")
ClientType=Request.Form("ClientType")
ClientName_=Request.Form("ClientName_")
ClientDate=Request.Form("ClientDate")
ClientName=Request.Form("ClientName")
ClientMan=Request.Form("ClientMan")
ClientMan_=Request.Form("ClientMan_")
ClientTel=Request.Form("ClientTel")
ClientTel_=Request.Form("ClientTel_")
ClientMobile=Request.Form("ClientMobile")
ClientFax=Request.Form("ClientFax")
ClientEmail=Request.Form("ClientEmail")
ClientAdd=Request.Form("ClientAdd")
ClientBeizu=Request.Form("ClientBeizu")
ClientDel=Request.Form("ClientDel")
call new_(rs)
rs.open "客户档案",conn,1,3
rs.addnew
rs("客户ID")=ClientID
rs("类别")=ClientType
rs("简称")=ClientName_
rs("建档日期")=ClientDate
rs("全称")=ClientName
rs("联系人")=ClientMan
rs("联系人称谓")=ClientMan_
rs("电话")=ClientTel
rs("分机")=ClientTel_
rs("手机")=ClientMobile
rs("传真")=ClientFax
rs("Email")=ClientEmail
rs("地址")=ClientAdd
rs("备注")=ClientBeizu
if ClientDel="true" then
rs("中止")=true
else
rs("中止")=false
end if
rs.update
call close_(rs)
response.Redirect("ClientMange.asp?ClientID="&ClientID&"&act=ClientID")
12 个解决方案
#1
你这个是返回的当前页?
查看你这一页是否有循环执行这个程序
查看你这一页是否有循环执行这个程序
#2
返回当前页面了吧!
#3
没有返回当前页,这个页clientAddnew专门用来增加,返回的是另外一个页面,如果这些代码没问题,那儿全贴出来看看
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="conn.asp" -->
<!--#include file="sub.asp" -->
<%
'---获取页面参数---
act=int(request("act"))
act_=request("act_")
'---页面操作---
select case act_
case "new"
ClientID=Request.Form("ClientID")
ClientType=Request.Form("ClientType")
ClientName_=Request.Form("ClientName_")
ClientDate=Request.Form("ClientDate")
ClientName=Request.Form("ClientName")
ClientMan=Request.Form("ClientMan")
ClientMan_=Request.Form("ClientMan_")
ClientTel=Request.Form("ClientTel")
ClientTel_=Request.Form("ClientTel_")
ClientMobile=Request.Form("ClientMobile")
ClientFax=Request.Form("ClientFax")
ClientEmail=Request.Form("ClientEmail")
ClientAdd=Request.Form("ClientAdd")
ClientBeizu=Request.Form("ClientBeizu")
ClientDel=Request.Form("ClientDel")
call new_(rs)
rs.open "客户档案",conn,1,3
rs.addnew
rs("客户ID")=ClientID
rs("类别")=ClientType
rs("简称")=ClientName_
rs("建档日期")=ClientDate
rs("全称")=ClientName
rs("联系人")=ClientMan
rs("联系人称谓")=ClientMan_
rs("电话")=ClientTel
rs("分机")=ClientTel_
rs("手机")=ClientMobile
rs("传真")=ClientFax
rs("Email")=ClientEmail
rs("地址")=ClientAdd
rs("备注")=ClientBeizu
if ClientDel="true" then
rs("中止")=true
else
rs("中止")=false
end if
rs.update
call close_(rs)
response.Redirect("ClientMange.asp?ClientID="&ClientID&"&act=ClientID")
end select
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新增客户档案</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
a:visited {
color: #00F;
}
input.smallInput {
border:1 solid black;
FONT-SIZE: 9pt;
FONT-STYLE: normal;
FONT-VARIANT: normal;
FONT-WEIGHT: normal;
HEIGHT: 18px;
LINE-HEIGHT: normal
}
-->
</style></head>
<body bgColor="transparent">
<% select case act %>
<% case 0 %>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#cccccc">
<%'取出客户类别
sql="select ID,类别 from 客户类别"
set rs=conn.execute(sql)
DomoType=rs.GetRows
set rs=nothing
DomoTypeNum=UBound(DomoType,2)
%>
<form name="form" method="post" action="">
<tr>
<td bgcolor="#F1FFF0">
<table width="300" height="40" border="00" cellpadding="0" cellspacing="0">
<tr>
<td>选择客户类别:</td>
<td><select name="ClientType" id="ClientType" onChange="submit()">
<option value="">请选择</option>
<% for i=0 to DomoTypeNum %>
<option value="<%= DomoType(0,i) %>"><%= DomoType(1,i) %>客户</option>
<% next %>
</select>
<input name="act" type="hidden" id="act" value="1"></td>
<td>[管理]</td>
<td>[<a href="#" onClick="history.back();">返回</a>]</td>
</tr>
</table></td>
</tr>
</form>
</table>
<% case 1
ClientType=request.Form("ClientType")
MXClientID=RsValue("select top 1 客户ID from 客户档案 where 客户ID like '"&ClientType&"%' order by 客户ID desc",3,"客户ID")
if MXClientID<>0 then
ClientNewID=zero(cint(trim(replace(MXClientID,ClientType,"")))+1,3)
ClientNewID=ClientType&ClientNewID
else
ClientNewID=ClientType&"001"
end if %>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#cccccc">
<tr>
<td bgcolor="#F1FFF0"><table width="350" height="275" border="0" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="" onSubmit="return formCheck(this);">
<tr>
<td height="25" colspan="4">新增客户档案:[<a href="<%= GetUrl("page") %>">返回</a>]</td>
</tr>
<tr>
<td align="center">编 号:</td>
<td><input name="ClientID" type="text" class="smallInput" id="ClientID" size="13" readonly title="不可修改" value="<%= ClientNewID %>"></td>
<td align="center">类 别:</td>
<td><input name="ClientType" type="text" class="smallInput" id="ClientType" size="13" readonly title="不可修改" value="<%= RsValue("select 类别 from 客户类别 where ID='"&ClientType&"'",3,"类别") %>"></td>
</tr>
<tr>
<td align="center">简 称:</td>
<td><input name="ClientName_" type="text" class="smallInput" id="ClientName_" size="13"></td>
<td align="center">建档日期:</td>
<td><input name="ClientDate" type="text" class="smallInput" id="ClientDate" value="<%= date %>" size="13"></td>
</tr>
<tr>
<td align="center">全 称:</td>
<td colspan="3"><input name="ClientName" type="text" class="smallInput" id="ClientName" value="-" size="41" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">联系人:</td>
<td><input name="ClientMan" type="text" class="smallInput" id="name" value="-" size="13" <% call Tinput("8") %>></td>
<td align="center">称 谓:</td>
<td><input name="ClientMan_" type="text" class="smallInput" id="ClientMan_" value="-" size="13" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">电 话:</td>
<td><input name="ClientTel" type="text" class="smallInput" id="ClientTel" value="-" size="13" <% call Tinput("8") %>></td>
<td align="center">分 机:</td>
<td><input name="ClientTel_" type="text" class="smallInput" id="ClientTel_" value="-" size="8" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">手 机:</td>
<td><input name="ClientMobile" type="text" class="smallInput" id="ClientMobile" value="-" size="13" <% call Tinput("8") %>></td>
<td align="center">传 真:</td>
<td><input name="ClientFax" type="text" class="smallInput" id="ClientFax" value="-" size="13" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">Email:</td>
<td colspan="3"><input name="ClientEmail" type="text" class="smallInput" id="ClientEmail" value="-" size="27" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">地 址:</td>
<td colspan="3"><textarea name="ClientAdd" cols="25" rows="2" class="smallInput" id="ClientAdd" <% call Tinput("8") %>>-</textarea></td>
</tr>
<tr>
<td align="center">备 注:</td>
<td colspan="3"><textarea name="ClientBeizu" cols="25" rows="2" class="smallInput" id="ClientBeizu" <% call Tinput("8") %>>-</textarea></td>
</tr>
<tr>
<td align="center">中 止:</td>
<td colspan="3"><input name="ClientDel" type="radio" id="radio" value="true">
是
<input name="ClientDel" type="radio" id="radio2" value="false" checked>
否</td>
</tr>
<tr>
<td colspan="4" align="center"><input name="act_" type="hidden" id="act_" value="new">
<input type="submit" name="button" id="button" value="增加"></td>
</tr>
</form>
</table></td>
</tr>
</table>
<% end select %>
</body>
</html>
#4
formCheck函数在哪里?看看里面是否多submit了一次
#5
把rs.addnew去掉试试
#6
call new_(rs)
rs.open "客户档案",conn,1,3
rs.addnew
这里有问题。new_(rs)是什么?
rs.open "客户档案",conn,1,3
rs.addnew
这里有问题。new_(rs)是什么?
#7
call new_(rs)
?????
?????
#8
call new_(rs)是我自建的函数,用以实例化一个recordset对象,问题已经解决,确实如toury所讲,我一直注意程序本身的逻辑是否存在问题,而忽略了这个表单验证函数formCheck,问题就出在这里,重新换了一个通用的表单验证函数,使用比较方便,惟一的问题就是如果是用按钮提交表单,就可以触发验证,而用链接提交表单时,就不能触发了,贴出给大家分享,顺便看看能不能解决这个问题:
未完待续
////////////////////////////////////////////////////////////////////////////////
/*
*--------------- 客户端表单通用验证checkForm(oForm) -----------------
* 本程序最初是由wanghr100(灰豆宝宝.net)的checkForm基础上进行修改的,增加了很多功能,如下:
* 1.对非ie的支持
* 2.增加了内置表达式和内置提示
* 3.增加了显示方式(弹出式和页面显示式)
* 4.增加了显示一条和显示全部
* 5.进行了封装(CLASS_CHECK)
* 6.支持外接函数或表达式(应用在密码一致)
* 7.简化了调用方式,所有操作只需要<script language='javascript' src='checkform.js'>,
然后在HTML里定义各标签验证格式
* 8.对IE增加了对键盘输入的限制(如:定义usage='int'时,输入框只能输入数字(非IE无效)
* 9.增加了对disabled的不验证
* 10.自定义报警方式(重写showMessageEx方法)
* 11.能对多FORM验证,还有针对普通按钮(非submit)下的验证
*-----------------------------申明信息-----------------------------
* 版本: 0.13
* 作者: ttyp
* 邮箱: ttyp@21cn.com
* 博客: http://www.cnblogs.com/ttyp/
* 声明: 对本程序可以任意复制传播,但请保留这些声明,对于内置的表达式有些没有做到很严格,如果你
有好的建议和意见,欢迎邮件和我联系或者上我的博客留言
* 日期: 2005-9-20
* 简介:
本程序只需要对需要验证的标签设置三个属性:usage,exp,tip
usage : 内置格式或表达式或函数
exp : 正则表达式(注意如果指定了usage则忽略exp)
tip : 出错提示(如果是内置格式可以不要此属性,有缺省提示)
调用时只需要引用<script language='javascript' src='checkform.js'>,然后为每个标记
增加以上3个属性(不一定需要全部)
* 原作者: wanghr100(灰豆宝宝.net)
* email: wanghr100@126.com
*
*--------------- 客户端表单通用验证checkForm(oForm) -----------------
*/
////////////////////////////////////////////////////////////////////////////////
function CLASS_CHECK(){
this.pass = true; //是否通过验证
this.showAll = true; //是否显示所有的验证错误
this.alert = true; //报警方式(默认alert报警)
this.message = ""; //错误内容
this.first = null; //在显示全部验证错误时的第一个错误控件(用于回到焦点)
this.cancel = false;
//定义内置格式
var aUsage = {
"int":"^([+-]?)\\d+$", //整数
"int+":"^([+]?)\\d+$", //正整数
"int-":"^-\\d+$", //负整数
"num":"^([+-]?)\\d*\\.?\\d+$", //数字
"num+":"^([+]?)\\d*\\.?\\d+$", //正数
"num-":"^-\\d*\\.?\\d+$", //负数
"float":"^([+-]?)\\d*\\.\\d+$", //浮点数
"float+":"^([+]?)\\d*\\.\\d+$", //正浮点数
"float-":"^-\\d*\\.\\d+$", //负浮点数
//邮件
"email":"^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$",
"color":"^#[a-fA-F0-9]{6}", //颜色
"url":"^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //联接
"chinese":"^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$", //仅中文
"ascii":"^[\\x00-\\xFF]+$", //仅ACSII字符
"zipcode":"^\\d{6}$", //邮编
"mobile":"^0{0,1}13[0-9]{9}$", //手机
"ip4":"^\(([0-1]?\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]?\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]?\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]?\\d{0,2})|(2[0-5]{0,2}))$", //ip地址
"notempty":"^[^ ]+$", //非空
"picture":"(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
"rar":"(.*)\\.(rar|zip|7zip|tgz)$", //压缩文件
"date":"^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$" //日期
};
//缺省消息
var aMessage = {
"int" :"请输入整数", //整数
"int+" :"请输入正整数", //正整数
"int-" :"请输入负整数", //负整数
"num" :"请输入数字", //数字
"num+" :"请输入正数", //正数
"num-" :"请输入负整数", //负数
"float" :"请输入浮点数", //浮点数
"float+":"请输入正浮点数", //正浮点数
"float-":"请输入负浮点数", //负浮点数
"email" :"请输入正确的邮箱地址", //邮件
"color" :"请输入正确的颜色", //颜色
"url" :"请输入正确的连接地址", //联接
"chinese":"请输入中文", //中文
"ascii" :"请输入ascii字符", //仅ACSII字符
"zipcode":"请输入正确的邮政编码", //邮编
"mobile":"请输入正确的手机号码", //手机
"ip4" :"请输入正确的IP地址", //ip地址
"notempty":"不能为空", //非空
"picture":"请选择图片", //图片
"rar" :"请输入压缩文件", //压缩文件
"date" :"请输入正确的日期" //日期
}
var me = this;
me.checkForm = function(oForm){
me.pass = true;
me.message = "";
me.first = null;
if(me.cancel==true){
return true;
}
var els = oForm.elements;
//遍历所有表元素
for(var i=0;i<els.length;i++){
//取得格式
var sUsage = els[i].getAttribute("Usage");
var sReg = "";
//如果设置Usage,则使用内置正则表达式,忽略Exp
if(typeof(sUsage)!="undefined"&&sUsage!=null){
//如果Usage在表达室里找到,则使用内置表达式,无则认为是表达式;表达式可以是函数;
if(aUsage[sUsage]!=null){
sReg = aUsage[sUsage];
} else {
try {
if(eval(sUsage)==false){
me.pass = false;
if(me.first==null){
me.first = els[i];
}
addMessage(getMessage(els[i]));
if(me.showAll==false){
setFocus(els[i]);
break;
}
}
} catch(e){
alert("表达式[" + sUsage +"]错误:" + e.description)
return false;
}
}
} else {
sReg = els[i].getAttribute("Exp");
}
if(typeof(sReg)!="undefined"&&sReg!=null){
//对于失效状态不验证
if(isDisabled(els[i])==true){
continue;
}
//取得表单的值,用通用取值函数
var sVal = getValue(els[i]);
//字符串->正则表达式,不区分大小写
var reg = new RegExp(sReg,"i");
if(!reg.test(sVal)){
me.pass = false;
if(me.first==null){
me.first = els[i];
}
未完待续
#9
续...
//alert(reg);
//验证不通过,弹出提示warning
var sTip = getMessage(els[i]);
if(sTip.length==0&&typeof(sUsage)!="undefined"&&sUsage!=null&&aMessage[sUsage]!=null){
sTip = aMessage[sUsage];
}
addMessage(sTip);
if(me.showAll==false){
//该表单元素取得焦点,用通用返回函数
setFocus(els[i]);
break;
}
}
}
}
if(me.pass==false){
showMessage();
if(me.first!=null&&me.showAll==true){
setFocus(me.first);
}
}
return me.pass;
}
/*
* 添加错误信息
*/
function addMessage(msg){
if(me.alert==true){
me.message += msg + "\n";
} else {
me.message += msg + "<br>";
}
}
/*
* 显示错误
*/
function getMessage(els){
var sTip = els.getAttribute("tip");
if(typeof(sTip)!="undefined"&&sTip!=null){
return sTip;
} else {
return "";
}
}
/*
* 显示错误
*/
function showMessage(){
//外接显示错误函数
if(typeof(me.showMessageEx)=="function"){
return me.showMessageEx(me.message);
}
if(me.alert==true){
alert(me.message);
} else {
var divTip;
divTip = document.getElementById("divErrorMessage");
try {
if(typeof(divTip)=="undefined"||divTip==null){
divTip = document.createElement("div");
divTip.id = "divErrorMessage";
divTip.name = "divErrorMessage";
divTip.style.color = "red";
document.body.appendChild(divTip);
}
divTip.innerHTML = me.message;
}catch(e){}
}
}
/*
* 获得元素是否失效(失效的元素不做判断)
*/
function isDisabled(el){
//对于radio,checkbox元素,只要其中有一个非失效元素就验证
if(el.type=="radio"||el.type=="checkbox"){
//取得第一个元素的name,搜索这个元素组
var tmpels = document.getElementsByName(el.name);
for(var i=0;i<tmpels.length;i++){
if(tmpels[i].disabled==false){
return false;
}
}
return true;
}else{
return el.disabled;
}
}
/*
* 取得对象的值(对于单选多选框把其选择的个数作为需要验证的值)
*/
function getValue(el){
//取得表单元素的类型
var sType = el.type;
switch(sType){
//文本输入框,直接取值el.value
case "text":
case "hidden":
case "password":
case "file":
case "textarea": return el.value;
//单多下拉菜单,遍历所有选项取得被选中的个数返回结果"0"表示选中一个,"00"表示选中两个
case "checkbox":
case "radio": return getRadioValue(el);
case "select-one":
case "select-multiple": return getSelectValue(el);
}
//取得radio,checkbox的选中数,用"0"来表示选中的个数,我们写正则的时候就可以通过0{1,}来表示选中个数
function getRadioValue(el){
var sValue = "";
//取得第一个元素的name,搜索这个元素组
var tmpels = document.getElementsByName(el.name);
for(var i=0;i<tmpels.length;i++){
if(tmpels[i].checked){
sValue += "0";
}
}
return sValue;
}
//取得select的选中数,用"0"来表示选中的个数,我们写正则的时候就可以通过0{1,}来表示选中个数
function getSelectValue(el){
var sValue = "";
for(var i=0;i<el.options.length;i++){
//单选下拉框提示选项设置为value=""
if(el.options[i].selected && el.options[i].value!=""){
sValue += "0";
}
}
return sValue;
}
}
/*
* 对没有通过验证的元素设置焦点
*/
function setFocus(el){
//取得表单元素的类型
var sType = el.type;
switch(sType){
//文本输入框,光标定位在文本输入框的末尾
case "text":
case "hidden":
case "password":
case "file":
case "textarea":
try{el.focus();var rng = el.createTextRange(); rng.collapse(false); rng.select();}catch(e){};
break;
//单多选,第一选项非失效控件取得焦点
case "checkbox":
case "radio":
var els = document.getElementsByName(el.name);
for(var i=0;i<els.length;i++){
if(els[i].disabled == false){
els[i].focus();
break;
}
}
break;
case "select-one":
case "select-multiple":
el.focus();
break;
}
}
#10
再续..
//自动绑定到所有form的onsubmit事件
if(window.attachEvent){
window.attachEvent("onload",function()
{
for(var i=0;i<document.forms.length;i++){
var theFrom = document.forms[i];
function mapping(f){
f.attachEvent("onsubmit",function(){return me.checkForm(f);});
}
if(theFrom){
mapping(theFrom);
theFrom.attachEvent("onclick",function(){
var o = event.srcElement;
if(typeof(o.type)!="undefined"){
var check = o.getAttribute("check");
if(typeof(check)!="undefined"&&check!=null&&check.toLowerCase()=="false"){
me.cancel = true;
}
}
}
);
}
}
}
);
}
else
{
window.onsubmit = function(e){var theFrom = e.target;if(theFrom){return me.checkForm(theFrom);}}
window.addEventListener("click",function(e){var o = e.target;
if(typeof(o.type)!="undefined"){
var check = o.getAttribute("check");
if(typeof(check)!="undefined"&&check!=null&&check.toLowerCase()=="false"){me.cancel = true;}
}
}
,false);
}
this.keyCheck = function(){
if(window.attachEvent){
window.attachEvent("onload",function(){for(var i=0;i<document.forms.length;i++){var theFrom = document.forms[i]; if(theFrom){myKeyCheck(theFrom);}}});
}else{
window.addEventListener("load",function(e){for(var i=0;i<document.forms.length;i++){var theFrom = document.forms[i]; if(theFrom){myKeyCheck(theFrom);}}},false);
}
function myKeyCheck(oForm){
var els = oForm.elements;
//遍历所有表元素
for(var i=0;i<els.length;i++){
//取得格式
var sUsage = els[i].getAttribute("Usage");
//如果设置Usage,则使用内置正则表达式,忽略Exp
if(typeof(sUsage)!="undefined"&&sUsage!=null){
switch(sUsage.toLowerCase ()){
case "zipcode":
case "int":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('+')<0?String.fromCharCode(chr)=="+":false)||(this.value.indexOf('-')<0?String.fromCharCode(chr)=="-":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/\D/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "mobile":
case "int+":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('+')<0?String.fromCharCode(chr)=="+":false);}
els[i].onpaste = function(e){if(e==null)if(e==null)return !clipboardData.getData('text').match(/[^\d\+]/); else return false; else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "int-":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('-')<0?String.fromCharCode(chr)=="-":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float":
case "num":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\+\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\+\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float+":
case "num+":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\+\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\+]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float-":
case "num-":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "ascii":
els[i].style.imeMode= "disabled";
break;
case "ip4":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
els[i].maxLength = 15;
break;
case "color":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[a-fA-Z]|\d/.test(String.fromCharCode(chr))||(this.value.indexOf('#')<0?String.fromCharCode(chr)=="#":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\#]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].maxLength = 7;
els[i].style.imeMode= "disabled";
break;
case "date":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\/\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
}
}
}
}
}
}
//初始化
var g_check = new CLASS_CHECK();
g_check.keyCheck();
#11
再续..
//自动绑定到所有form的onsubmit事件
if(window.attachEvent){
window.attachEvent("onload",function()
{
for(var i=0;i<document.forms.length;i++){
var theFrom = document.forms[i];
function mapping(f){
f.attachEvent("onsubmit",function(){return me.checkForm(f);});
}
if(theFrom){
mapping(theFrom);
theFrom.attachEvent("onclick",function(){
var o = event.srcElement;
if(typeof(o.type)!="undefined"){
var check = o.getAttribute("check");
if(typeof(check)!="undefined"&&check!=null&&check.toLowerCase()=="false"){
me.cancel = true;
}
}
}
);
}
}
}
);
}
else
{
window.onsubmit = function(e){var theFrom = e.target;if(theFrom){return me.checkForm(theFrom);}}
window.addEventListener("click",function(e){var o = e.target;
if(typeof(o.type)!="undefined"){
var check = o.getAttribute("check");
if(typeof(check)!="undefined"&&check!=null&&check.toLowerCase()=="false"){me.cancel = true;}
}
}
,false);
}
this.keyCheck = function(){
if(window.attachEvent){
window.attachEvent("onload",function(){for(var i=0;i<document.forms.length;i++){var theFrom = document.forms[i]; if(theFrom){myKeyCheck(theFrom);}}});
}else{
window.addEventListener("load",function(e){for(var i=0;i<document.forms.length;i++){var theFrom = document.forms[i]; if(theFrom){myKeyCheck(theFrom);}}},false);
}
function myKeyCheck(oForm){
var els = oForm.elements;
//遍历所有表元素
for(var i=0;i<els.length;i++){
//取得格式
var sUsage = els[i].getAttribute("Usage");
//如果设置Usage,则使用内置正则表达式,忽略Exp
if(typeof(sUsage)!="undefined"&&sUsage!=null){
switch(sUsage.toLowerCase ()){
case "zipcode":
case "int":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('+')<0?String.fromCharCode(chr)=="+":false)||(this.value.indexOf('-')<0?String.fromCharCode(chr)=="-":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/\D/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "mobile":
case "int+":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('+')<0?String.fromCharCode(chr)=="+":false);}
els[i].onpaste = function(e){if(e==null)if(e==null)return !clipboardData.getData('text').match(/[^\d\+]/); else return false; else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "int-":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('-')<0?String.fromCharCode(chr)=="-":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float":
case "num":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\+\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\+\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float+":
case "num+":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\+\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\+]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float-":
case "num-":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "ascii":
els[i].style.imeMode= "disabled";
break;
case "ip4":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
els[i].maxLength = 15;
break;
case "color":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[a-fA-Z]|\d/.test(String.fromCharCode(chr))||(this.value.indexOf('#')<0?String.fromCharCode(chr)=="#":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\#]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].maxLength = 7;
els[i].style.imeMode= "disabled";
break;
case "date":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\/\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
}
}
}
}
}
}
//初始化
var g_check = new CLASS_CHECK();
g_check.keyCheck();
#12
谢谢楼上分析,我也受益了.
#1
你这个是返回的当前页?
查看你这一页是否有循环执行这个程序
查看你这一页是否有循环执行这个程序
#2
返回当前页面了吧!
#3
没有返回当前页,这个页clientAddnew专门用来增加,返回的是另外一个页面,如果这些代码没问题,那儿全贴出来看看
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="conn.asp" -->
<!--#include file="sub.asp" -->
<%
'---获取页面参数---
act=int(request("act"))
act_=request("act_")
'---页面操作---
select case act_
case "new"
ClientID=Request.Form("ClientID")
ClientType=Request.Form("ClientType")
ClientName_=Request.Form("ClientName_")
ClientDate=Request.Form("ClientDate")
ClientName=Request.Form("ClientName")
ClientMan=Request.Form("ClientMan")
ClientMan_=Request.Form("ClientMan_")
ClientTel=Request.Form("ClientTel")
ClientTel_=Request.Form("ClientTel_")
ClientMobile=Request.Form("ClientMobile")
ClientFax=Request.Form("ClientFax")
ClientEmail=Request.Form("ClientEmail")
ClientAdd=Request.Form("ClientAdd")
ClientBeizu=Request.Form("ClientBeizu")
ClientDel=Request.Form("ClientDel")
call new_(rs)
rs.open "客户档案",conn,1,3
rs.addnew
rs("客户ID")=ClientID
rs("类别")=ClientType
rs("简称")=ClientName_
rs("建档日期")=ClientDate
rs("全称")=ClientName
rs("联系人")=ClientMan
rs("联系人称谓")=ClientMan_
rs("电话")=ClientTel
rs("分机")=ClientTel_
rs("手机")=ClientMobile
rs("传真")=ClientFax
rs("Email")=ClientEmail
rs("地址")=ClientAdd
rs("备注")=ClientBeizu
if ClientDel="true" then
rs("中止")=true
else
rs("中止")=false
end if
rs.update
call close_(rs)
response.Redirect("ClientMange.asp?ClientID="&ClientID&"&act=ClientID")
end select
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>新增客户档案</title>
<style type="text/css">
<!--
body,td,th {
font-size: 12px;
}
a:visited {
color: #00F;
}
input.smallInput {
border:1 solid black;
FONT-SIZE: 9pt;
FONT-STYLE: normal;
FONT-VARIANT: normal;
FONT-WEIGHT: normal;
HEIGHT: 18px;
LINE-HEIGHT: normal
}
-->
</style></head>
<body bgColor="transparent">
<% select case act %>
<% case 0 %>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#cccccc">
<%'取出客户类别
sql="select ID,类别 from 客户类别"
set rs=conn.execute(sql)
DomoType=rs.GetRows
set rs=nothing
DomoTypeNum=UBound(DomoType,2)
%>
<form name="form" method="post" action="">
<tr>
<td bgcolor="#F1FFF0">
<table width="300" height="40" border="00" cellpadding="0" cellspacing="0">
<tr>
<td>选择客户类别:</td>
<td><select name="ClientType" id="ClientType" onChange="submit()">
<option value="">请选择</option>
<% for i=0 to DomoTypeNum %>
<option value="<%= DomoType(0,i) %>"><%= DomoType(1,i) %>客户</option>
<% next %>
</select>
<input name="act" type="hidden" id="act" value="1"></td>
<td>[管理]</td>
<td>[<a href="#" onClick="history.back();">返回</a>]</td>
</tr>
</table></td>
</tr>
</form>
</table>
<% case 1
ClientType=request.Form("ClientType")
MXClientID=RsValue("select top 1 客户ID from 客户档案 where 客户ID like '"&ClientType&"%' order by 客户ID desc",3,"客户ID")
if MXClientID<>0 then
ClientNewID=zero(cint(trim(replace(MXClientID,ClientType,"")))+1,3)
ClientNewID=ClientType&ClientNewID
else
ClientNewID=ClientType&"001"
end if %>
<table border="1" cellpadding="0" cellspacing="0" bordercolor="#cccccc">
<tr>
<td bgcolor="#F1FFF0"><table width="350" height="275" border="0" cellpadding="0" cellspacing="0">
<form name="form1" method="post" action="" onSubmit="return formCheck(this);">
<tr>
<td height="25" colspan="4">新增客户档案:[<a href="<%= GetUrl("page") %>">返回</a>]</td>
</tr>
<tr>
<td align="center">编 号:</td>
<td><input name="ClientID" type="text" class="smallInput" id="ClientID" size="13" readonly title="不可修改" value="<%= ClientNewID %>"></td>
<td align="center">类 别:</td>
<td><input name="ClientType" type="text" class="smallInput" id="ClientType" size="13" readonly title="不可修改" value="<%= RsValue("select 类别 from 客户类别 where ID='"&ClientType&"'",3,"类别") %>"></td>
</tr>
<tr>
<td align="center">简 称:</td>
<td><input name="ClientName_" type="text" class="smallInput" id="ClientName_" size="13"></td>
<td align="center">建档日期:</td>
<td><input name="ClientDate" type="text" class="smallInput" id="ClientDate" value="<%= date %>" size="13"></td>
</tr>
<tr>
<td align="center">全 称:</td>
<td colspan="3"><input name="ClientName" type="text" class="smallInput" id="ClientName" value="-" size="41" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">联系人:</td>
<td><input name="ClientMan" type="text" class="smallInput" id="name" value="-" size="13" <% call Tinput("8") %>></td>
<td align="center">称 谓:</td>
<td><input name="ClientMan_" type="text" class="smallInput" id="ClientMan_" value="-" size="13" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">电 话:</td>
<td><input name="ClientTel" type="text" class="smallInput" id="ClientTel" value="-" size="13" <% call Tinput("8") %>></td>
<td align="center">分 机:</td>
<td><input name="ClientTel_" type="text" class="smallInput" id="ClientTel_" value="-" size="8" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">手 机:</td>
<td><input name="ClientMobile" type="text" class="smallInput" id="ClientMobile" value="-" size="13" <% call Tinput("8") %>></td>
<td align="center">传 真:</td>
<td><input name="ClientFax" type="text" class="smallInput" id="ClientFax" value="-" size="13" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">Email:</td>
<td colspan="3"><input name="ClientEmail" type="text" class="smallInput" id="ClientEmail" value="-" size="27" <% call Tinput("8") %>></td>
</tr>
<tr>
<td align="center">地 址:</td>
<td colspan="3"><textarea name="ClientAdd" cols="25" rows="2" class="smallInput" id="ClientAdd" <% call Tinput("8") %>>-</textarea></td>
</tr>
<tr>
<td align="center">备 注:</td>
<td colspan="3"><textarea name="ClientBeizu" cols="25" rows="2" class="smallInput" id="ClientBeizu" <% call Tinput("8") %>>-</textarea></td>
</tr>
<tr>
<td align="center">中 止:</td>
<td colspan="3"><input name="ClientDel" type="radio" id="radio" value="true">
是
<input name="ClientDel" type="radio" id="radio2" value="false" checked>
否</td>
</tr>
<tr>
<td colspan="4" align="center"><input name="act_" type="hidden" id="act_" value="new">
<input type="submit" name="button" id="button" value="增加"></td>
</tr>
</form>
</table></td>
</tr>
</table>
<% end select %>
</body>
</html>
#4
formCheck函数在哪里?看看里面是否多submit了一次
#5
把rs.addnew去掉试试
#6
call new_(rs)
rs.open "客户档案",conn,1,3
rs.addnew
这里有问题。new_(rs)是什么?
rs.open "客户档案",conn,1,3
rs.addnew
这里有问题。new_(rs)是什么?
#7
call new_(rs)
?????
?????
#8
call new_(rs)是我自建的函数,用以实例化一个recordset对象,问题已经解决,确实如toury所讲,我一直注意程序本身的逻辑是否存在问题,而忽略了这个表单验证函数formCheck,问题就出在这里,重新换了一个通用的表单验证函数,使用比较方便,惟一的问题就是如果是用按钮提交表单,就可以触发验证,而用链接提交表单时,就不能触发了,贴出给大家分享,顺便看看能不能解决这个问题:
未完待续
////////////////////////////////////////////////////////////////////////////////
/*
*--------------- 客户端表单通用验证checkForm(oForm) -----------------
* 本程序最初是由wanghr100(灰豆宝宝.net)的checkForm基础上进行修改的,增加了很多功能,如下:
* 1.对非ie的支持
* 2.增加了内置表达式和内置提示
* 3.增加了显示方式(弹出式和页面显示式)
* 4.增加了显示一条和显示全部
* 5.进行了封装(CLASS_CHECK)
* 6.支持外接函数或表达式(应用在密码一致)
* 7.简化了调用方式,所有操作只需要<script language='javascript' src='checkform.js'>,
然后在HTML里定义各标签验证格式
* 8.对IE增加了对键盘输入的限制(如:定义usage='int'时,输入框只能输入数字(非IE无效)
* 9.增加了对disabled的不验证
* 10.自定义报警方式(重写showMessageEx方法)
* 11.能对多FORM验证,还有针对普通按钮(非submit)下的验证
*-----------------------------申明信息-----------------------------
* 版本: 0.13
* 作者: ttyp
* 邮箱: ttyp@21cn.com
* 博客: http://www.cnblogs.com/ttyp/
* 声明: 对本程序可以任意复制传播,但请保留这些声明,对于内置的表达式有些没有做到很严格,如果你
有好的建议和意见,欢迎邮件和我联系或者上我的博客留言
* 日期: 2005-9-20
* 简介:
本程序只需要对需要验证的标签设置三个属性:usage,exp,tip
usage : 内置格式或表达式或函数
exp : 正则表达式(注意如果指定了usage则忽略exp)
tip : 出错提示(如果是内置格式可以不要此属性,有缺省提示)
调用时只需要引用<script language='javascript' src='checkform.js'>,然后为每个标记
增加以上3个属性(不一定需要全部)
* 原作者: wanghr100(灰豆宝宝.net)
* email: wanghr100@126.com
*
*--------------- 客户端表单通用验证checkForm(oForm) -----------------
*/
////////////////////////////////////////////////////////////////////////////////
function CLASS_CHECK(){
this.pass = true; //是否通过验证
this.showAll = true; //是否显示所有的验证错误
this.alert = true; //报警方式(默认alert报警)
this.message = ""; //错误内容
this.first = null; //在显示全部验证错误时的第一个错误控件(用于回到焦点)
this.cancel = false;
//定义内置格式
var aUsage = {
"int":"^([+-]?)\\d+$", //整数
"int+":"^([+]?)\\d+$", //正整数
"int-":"^-\\d+$", //负整数
"num":"^([+-]?)\\d*\\.?\\d+$", //数字
"num+":"^([+]?)\\d*\\.?\\d+$", //正数
"num-":"^-\\d*\\.?\\d+$", //负数
"float":"^([+-]?)\\d*\\.\\d+$", //浮点数
"float+":"^([+]?)\\d*\\.\\d+$", //正浮点数
"float-":"^-\\d*\\.\\d+$", //负浮点数
//邮件
"email":"^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$",
"color":"^#[a-fA-F0-9]{6}", //颜色
"url":"^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$", //联接
"chinese":"^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$", //仅中文
"ascii":"^[\\x00-\\xFF]+$", //仅ACSII字符
"zipcode":"^\\d{6}$", //邮编
"mobile":"^0{0,1}13[0-9]{9}$", //手机
"ip4":"^\(([0-1]?\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]?\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]?\\d{0,2})|(2[0-5]{0,2}))\\.(([0-1]?\\d{0,2})|(2[0-5]{0,2}))$", //ip地址
"notempty":"^[^ ]+$", //非空
"picture":"(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$", //图片
"rar":"(.*)\\.(rar|zip|7zip|tgz)$", //压缩文件
"date":"^\\d{4}(\\-|\\/|\.)\\d{1,2}\\1\\d{1,2}$" //日期
};
//缺省消息
var aMessage = {
"int" :"请输入整数", //整数
"int+" :"请输入正整数", //正整数
"int-" :"请输入负整数", //负整数
"num" :"请输入数字", //数字
"num+" :"请输入正数", //正数
"num-" :"请输入负整数", //负数
"float" :"请输入浮点数", //浮点数
"float+":"请输入正浮点数", //正浮点数
"float-":"请输入负浮点数", //负浮点数
"email" :"请输入正确的邮箱地址", //邮件
"color" :"请输入正确的颜色", //颜色
"url" :"请输入正确的连接地址", //联接
"chinese":"请输入中文", //中文
"ascii" :"请输入ascii字符", //仅ACSII字符
"zipcode":"请输入正确的邮政编码", //邮编
"mobile":"请输入正确的手机号码", //手机
"ip4" :"请输入正确的IP地址", //ip地址
"notempty":"不能为空", //非空
"picture":"请选择图片", //图片
"rar" :"请输入压缩文件", //压缩文件
"date" :"请输入正确的日期" //日期
}
var me = this;
me.checkForm = function(oForm){
me.pass = true;
me.message = "";
me.first = null;
if(me.cancel==true){
return true;
}
var els = oForm.elements;
//遍历所有表元素
for(var i=0;i<els.length;i++){
//取得格式
var sUsage = els[i].getAttribute("Usage");
var sReg = "";
//如果设置Usage,则使用内置正则表达式,忽略Exp
if(typeof(sUsage)!="undefined"&&sUsage!=null){
//如果Usage在表达室里找到,则使用内置表达式,无则认为是表达式;表达式可以是函数;
if(aUsage[sUsage]!=null){
sReg = aUsage[sUsage];
} else {
try {
if(eval(sUsage)==false){
me.pass = false;
if(me.first==null){
me.first = els[i];
}
addMessage(getMessage(els[i]));
if(me.showAll==false){
setFocus(els[i]);
break;
}
}
} catch(e){
alert("表达式[" + sUsage +"]错误:" + e.description)
return false;
}
}
} else {
sReg = els[i].getAttribute("Exp");
}
if(typeof(sReg)!="undefined"&&sReg!=null){
//对于失效状态不验证
if(isDisabled(els[i])==true){
continue;
}
//取得表单的值,用通用取值函数
var sVal = getValue(els[i]);
//字符串->正则表达式,不区分大小写
var reg = new RegExp(sReg,"i");
if(!reg.test(sVal)){
me.pass = false;
if(me.first==null){
me.first = els[i];
}
未完待续
#9
续...
//alert(reg);
//验证不通过,弹出提示warning
var sTip = getMessage(els[i]);
if(sTip.length==0&&typeof(sUsage)!="undefined"&&sUsage!=null&&aMessage[sUsage]!=null){
sTip = aMessage[sUsage];
}
addMessage(sTip);
if(me.showAll==false){
//该表单元素取得焦点,用通用返回函数
setFocus(els[i]);
break;
}
}
}
}
if(me.pass==false){
showMessage();
if(me.first!=null&&me.showAll==true){
setFocus(me.first);
}
}
return me.pass;
}
/*
* 添加错误信息
*/
function addMessage(msg){
if(me.alert==true){
me.message += msg + "\n";
} else {
me.message += msg + "<br>";
}
}
/*
* 显示错误
*/
function getMessage(els){
var sTip = els.getAttribute("tip");
if(typeof(sTip)!="undefined"&&sTip!=null){
return sTip;
} else {
return "";
}
}
/*
* 显示错误
*/
function showMessage(){
//外接显示错误函数
if(typeof(me.showMessageEx)=="function"){
return me.showMessageEx(me.message);
}
if(me.alert==true){
alert(me.message);
} else {
var divTip;
divTip = document.getElementById("divErrorMessage");
try {
if(typeof(divTip)=="undefined"||divTip==null){
divTip = document.createElement("div");
divTip.id = "divErrorMessage";
divTip.name = "divErrorMessage";
divTip.style.color = "red";
document.body.appendChild(divTip);
}
divTip.innerHTML = me.message;
}catch(e){}
}
}
/*
* 获得元素是否失效(失效的元素不做判断)
*/
function isDisabled(el){
//对于radio,checkbox元素,只要其中有一个非失效元素就验证
if(el.type=="radio"||el.type=="checkbox"){
//取得第一个元素的name,搜索这个元素组
var tmpels = document.getElementsByName(el.name);
for(var i=0;i<tmpels.length;i++){
if(tmpels[i].disabled==false){
return false;
}
}
return true;
}else{
return el.disabled;
}
}
/*
* 取得对象的值(对于单选多选框把其选择的个数作为需要验证的值)
*/
function getValue(el){
//取得表单元素的类型
var sType = el.type;
switch(sType){
//文本输入框,直接取值el.value
case "text":
case "hidden":
case "password":
case "file":
case "textarea": return el.value;
//单多下拉菜单,遍历所有选项取得被选中的个数返回结果"0"表示选中一个,"00"表示选中两个
case "checkbox":
case "radio": return getRadioValue(el);
case "select-one":
case "select-multiple": return getSelectValue(el);
}
//取得radio,checkbox的选中数,用"0"来表示选中的个数,我们写正则的时候就可以通过0{1,}来表示选中个数
function getRadioValue(el){
var sValue = "";
//取得第一个元素的name,搜索这个元素组
var tmpels = document.getElementsByName(el.name);
for(var i=0;i<tmpels.length;i++){
if(tmpels[i].checked){
sValue += "0";
}
}
return sValue;
}
//取得select的选中数,用"0"来表示选中的个数,我们写正则的时候就可以通过0{1,}来表示选中个数
function getSelectValue(el){
var sValue = "";
for(var i=0;i<el.options.length;i++){
//单选下拉框提示选项设置为value=""
if(el.options[i].selected && el.options[i].value!=""){
sValue += "0";
}
}
return sValue;
}
}
/*
* 对没有通过验证的元素设置焦点
*/
function setFocus(el){
//取得表单元素的类型
var sType = el.type;
switch(sType){
//文本输入框,光标定位在文本输入框的末尾
case "text":
case "hidden":
case "password":
case "file":
case "textarea":
try{el.focus();var rng = el.createTextRange(); rng.collapse(false); rng.select();}catch(e){};
break;
//单多选,第一选项非失效控件取得焦点
case "checkbox":
case "radio":
var els = document.getElementsByName(el.name);
for(var i=0;i<els.length;i++){
if(els[i].disabled == false){
els[i].focus();
break;
}
}
break;
case "select-one":
case "select-multiple":
el.focus();
break;
}
}
#10
再续..
//自动绑定到所有form的onsubmit事件
if(window.attachEvent){
window.attachEvent("onload",function()
{
for(var i=0;i<document.forms.length;i++){
var theFrom = document.forms[i];
function mapping(f){
f.attachEvent("onsubmit",function(){return me.checkForm(f);});
}
if(theFrom){
mapping(theFrom);
theFrom.attachEvent("onclick",function(){
var o = event.srcElement;
if(typeof(o.type)!="undefined"){
var check = o.getAttribute("check");
if(typeof(check)!="undefined"&&check!=null&&check.toLowerCase()=="false"){
me.cancel = true;
}
}
}
);
}
}
}
);
}
else
{
window.onsubmit = function(e){var theFrom = e.target;if(theFrom){return me.checkForm(theFrom);}}
window.addEventListener("click",function(e){var o = e.target;
if(typeof(o.type)!="undefined"){
var check = o.getAttribute("check");
if(typeof(check)!="undefined"&&check!=null&&check.toLowerCase()=="false"){me.cancel = true;}
}
}
,false);
}
this.keyCheck = function(){
if(window.attachEvent){
window.attachEvent("onload",function(){for(var i=0;i<document.forms.length;i++){var theFrom = document.forms[i]; if(theFrom){myKeyCheck(theFrom);}}});
}else{
window.addEventListener("load",function(e){for(var i=0;i<document.forms.length;i++){var theFrom = document.forms[i]; if(theFrom){myKeyCheck(theFrom);}}},false);
}
function myKeyCheck(oForm){
var els = oForm.elements;
//遍历所有表元素
for(var i=0;i<els.length;i++){
//取得格式
var sUsage = els[i].getAttribute("Usage");
//如果设置Usage,则使用内置正则表达式,忽略Exp
if(typeof(sUsage)!="undefined"&&sUsage!=null){
switch(sUsage.toLowerCase ()){
case "zipcode":
case "int":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('+')<0?String.fromCharCode(chr)=="+":false)||(this.value.indexOf('-')<0?String.fromCharCode(chr)=="-":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/\D/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "mobile":
case "int+":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('+')<0?String.fromCharCode(chr)=="+":false);}
els[i].onpaste = function(e){if(e==null)if(e==null)return !clipboardData.getData('text').match(/[^\d\+]/); else return false; else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "int-":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('-')<0?String.fromCharCode(chr)=="-":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float":
case "num":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\+\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\+\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float+":
case "num+":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\+\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\+]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float-":
case "num-":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "ascii":
els[i].style.imeMode= "disabled";
break;
case "ip4":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
els[i].maxLength = 15;
break;
case "color":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[a-fA-Z]|\d/.test(String.fromCharCode(chr))||(this.value.indexOf('#')<0?String.fromCharCode(chr)=="#":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\#]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].maxLength = 7;
els[i].style.imeMode= "disabled";
break;
case "date":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\/\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
}
}
}
}
}
}
//初始化
var g_check = new CLASS_CHECK();
g_check.keyCheck();
#11
再续..
//自动绑定到所有form的onsubmit事件
if(window.attachEvent){
window.attachEvent("onload",function()
{
for(var i=0;i<document.forms.length;i++){
var theFrom = document.forms[i];
function mapping(f){
f.attachEvent("onsubmit",function(){return me.checkForm(f);});
}
if(theFrom){
mapping(theFrom);
theFrom.attachEvent("onclick",function(){
var o = event.srcElement;
if(typeof(o.type)!="undefined"){
var check = o.getAttribute("check");
if(typeof(check)!="undefined"&&check!=null&&check.toLowerCase()=="false"){
me.cancel = true;
}
}
}
);
}
}
}
);
}
else
{
window.onsubmit = function(e){var theFrom = e.target;if(theFrom){return me.checkForm(theFrom);}}
window.addEventListener("click",function(e){var o = e.target;
if(typeof(o.type)!="undefined"){
var check = o.getAttribute("check");
if(typeof(check)!="undefined"&&check!=null&&check.toLowerCase()=="false"){me.cancel = true;}
}
}
,false);
}
this.keyCheck = function(){
if(window.attachEvent){
window.attachEvent("onload",function(){for(var i=0;i<document.forms.length;i++){var theFrom = document.forms[i]; if(theFrom){myKeyCheck(theFrom);}}});
}else{
window.addEventListener("load",function(e){for(var i=0;i<document.forms.length;i++){var theFrom = document.forms[i]; if(theFrom){myKeyCheck(theFrom);}}},false);
}
function myKeyCheck(oForm){
var els = oForm.elements;
//遍历所有表元素
for(var i=0;i<els.length;i++){
//取得格式
var sUsage = els[i].getAttribute("Usage");
//如果设置Usage,则使用内置正则表达式,忽略Exp
if(typeof(sUsage)!="undefined"&&sUsage!=null){
switch(sUsage.toLowerCase ()){
case "zipcode":
case "int":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('+')<0?String.fromCharCode(chr)=="+":false)||(this.value.indexOf('-')<0?String.fromCharCode(chr)=="-":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/\D/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "mobile":
case "int+":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('+')<0?String.fromCharCode(chr)=="+":false);}
els[i].onpaste = function(e){if(e==null)if(e==null)return !clipboardData.getData('text').match(/[^\d\+]/); else return false; else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "int-":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /\d/.test(String.fromCharCode(chr))||(this.value.indexOf('-')<0?String.fromCharCode(chr)=="-":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float":
case "num":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\+\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\+\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float+":
case "num+":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\+\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\+]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "float-":
case "num-":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
case "ascii":
els[i].style.imeMode= "disabled";
break;
case "ip4":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
els[i].maxLength = 15;
break;
case "color":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[a-fA-Z]|\d/.test(String.fromCharCode(chr))||(this.value.indexOf('#')<0?String.fromCharCode(chr)=="#":false);}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\#]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].maxLength = 7;
els[i].style.imeMode= "disabled";
break;
case "date":
els[i].onkeypress = function(e){var chr;if(e)chr=e.charCode; else chr=window.event.keyCode;if(chr==0)return true; else return /[\/\-\.]|\d/.test(String.fromCharCode(chr));}
els[i].onpaste = function(e){if(e==null)return !clipboardData.getData('text').match(/[^\d\.\-]/); else return false;}
els[i].ondragenter = function(e){return false;}
els[i].style.imeMode= "disabled";
break;
}
}
}
}
}
}
//初始化
var g_check = new CLASS_CHECK();
g_check.keyCheck();
#12
谢谢楼上分析,我也受益了.