<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
</HEAD>
<BODY>
<input type="text" name="t1" value=""><BR>
<span> </span><BR>
<input type="button" id="btn" value="clear" >
<SCRIPT LANGUAGE="JavaScript">
<!--
function init(){
var t1=document.getElementsByName("t1")[0];
var t2=document.getElementsByTagName("span")[0];
var t3=document.getElementsByTagName("input")[1];
//alert(t3.value);
t1.οnchange=function(){
var reg=/^([a-zA-Z0-9])+$/;
var num=/[0-9]/gi;
var en=/[a-zA-Z]/gi;
var v=this.value;
if(reg.test(v)){
var mNums=v.match(num)?v.match(num).length:0;
var mEns=v.match(en)?v.match(en).length:0;
t2.innerHTML="你输入了"+mNums+"个数字,"+mEns+"个字母";
}else{
t2.innerHTML="<font color='red'>输入错误</font>";
}
}
t3.οnclick=function(){
t2.innerHTML='';
t1.value='';
}
}
init();
//-->
</SCRIPT>
</BODY>
</HTML>
利用正则获取输入中的字母及数字个数
最新推荐文章于 2020-10-28 13:57:49 发布