87,922
社区成员
发帖
与我相关
我的任务
分享
<script language="javascript" type="text/javascript">
/*============================================================
函数功能: 根据ID返回对象
参数说明: objID 对象id
*/
function $(objID)
{
return document.getElementById(objID);
}
/*============================================================
函数功能: 绑定事件
参数说明: obj 对象 ; eventName 事件名称 ; eventFunction 事件处理程序 ; useCapture 用于W3C
*/
function addEventListener(obj,eventName,eventFunction,useCapture)
{
if (isIE())
{
eventName.substring(0,2) != "on" ? eventName = "on" + eventName : eventName = eventName; //click转onclick
obj.attachEvent(eventName,eventFunction);
}
else
{
eventName.substring(0,2) == "on" ? eventName = eventName.substring(2,eventName.length) : eventName = eventName; //onclick转click
obj.addEventListener(eventName,eventFunction,useCapture);
}
}
/*============================================================
函数功能: 标识,只能输入数字、字母、下划线、横线
参数说明: id 文本框id
*/
function onlyMark(id)
{
onlyInput(id,/^[0-9a-zA-Z_\-]+$/g);
}
/*============================================================
函数功能: 只能输入正则指定内容
参数说明: id 文本框ID ; source RegExp.source
*/
function onlyInput(id,source)
{
var txt = $(id);
if (txt == null) return; //对象不存在
var handleOnly = function ()
{
if (source.test(txt.value)) //不执行任何操作
{return false;window.alert("合法");}
else
{$("dt2").value = "执行了:" + txt.value + ":" + source.test(txt.value);}
}
if (isIE())
txt.attachEvent("onpropertychange",handleOnly);
else
txt.addEventListener("input",handleOnly,false);
}
</script>
<form id="soForm" name="soForm">
<input type="text" id="dt1" name="dt1" /><br />
<input type="text" id="dt2" name="dt2" />
<br />
</form>
<script language="javascript" type="text/javascript">
onlyMark("dt1");
</script>
source.lastIndex =0;
if (source.test(txt.value)) //不执行任何操作
<script language="javascript" type="text/javascript">
/*============================================================
函数功能: 根据ID返回对象
参数说明: objID 对象id
*/
function isIE(){ // 随便写的一个方法, 临时用下 :)
return document.attachEvent;
}
function $(objID)
{
return document.getElementById(objID);
}
/*============================================================
函数功能: 绑定事件
参数说明: obj 对象 ; eventName 事件名称 ; eventFunction 事件处理程序 ; useCapture 用于W3C
*/
function addEventListener(obj,eventName,eventFunction,useCapture)
{
if (isIE())
{
eventName.substring(0,2) != "on" ? eventName = "on" + eventName : eventName = eventName; //click转onclick
obj.attachEvent(eventName,eventFunction);
}
else
{
eventName.substring(0,2) == "on" ? eventName = eventName.substring(2,eventName.length) : eventName = eventName; //onclick转click
obj.addEventListener(eventName,eventFunction,useCapture);
}
}
/*============================================================
函数功能: 标识,只能输入数字、字母、下划线、横线
参数说明: id 文本框id
*/
function onlyMark(id)
{
onlyInput(id,/^[\w\-]+$/);
}
/*============================================================
函数功能: 只能输入正则指定内容
参数说明: id 文本框ID ; source RegExp.source
*/
function onlyInput(id,source)
{
var txt = $(id);
if (txt == null) return; //对象不存在
var handleOnly = function ()
{
if (source.test(txt.value)) //不执行任何操作
{return false;window.alert("合法");}
else
{$("dt2").value = "执行了:" + txt.value + ":" + source.test(txt.value);}
}
if (isIE())
txt.attachEvent("onpropertychange",handleOnly);
else
txt.addEventListener("input",handleOnly,false);
}
</script>
HTML code
<form id="soForm" name="soForm">
<input type="text" id="dt1" name="dt1" /><br />
<input type="text" id="dt2" name="dt2" />
<br />
</form>
<script language="javascript" type="text/javascript">
onlyMark("dt1");
</script>
function onlyMark(id)
{
onlyInput(id,/^[\w\-]+$/);
}