📄 13-13.txt
字号:
/*--------------- 客户端表单验证模块checkForm.js -----------------
* 可以对文本框进行合法性验证
* 在表单中可以这样使用,它提交之前调用checkForm(this)函数进行验证:
* <form name="myForm" onsubmit="javascript:return checkForm(this);">
* <input type="text" name="id" checkIt="正则表达式" tips="弹出的提示">
* <input type="submit">
* </form>
*----------------------------------------------------------------
*/
function checkForm(myForm){
//检查两次密码输入是否匹配
//if(myForm.user_password.value != myForm.user_repassword.value) {
//window.alert("密码不匹配!请检查");
//return false;
//}
//遍历表单控件
//检查form元素是否有子节点,从而确定邮箱中当前页是否存在邮件
//如果不存在就退出,否则,执行else
if(!myForm.hasChildNodes()){
return;
}else{
var myControls = myForm.childNodes;
for(var i=0;i<myControls.length;i++){
//检查控件是否存在checkIt属性,也就是是否需要验证
if(myControls[i].nodeName.toLowerCase() != "input"){continue;};
var type = myControls[i].getAttribute("type").toLowerCase();
if(type == "submit"){continue;};//对提交按钮和重置按钮不做验证
if(type == "reset"){continue;};
var checkIt = myControls[i].getAttribute("checkIt");
if(checkIt == null){continue;};
if(checkIt == ""){continue;};
//取得验证的正则字符串
var strReg = checkIt;
//取得表单的值,注意这里不能使用getAttribute获取值
var strVal = myControls[i].value;
//使用正则字符串定义一个RegExp对象 匹配时不区分大小写
var reg = new RegExp(strReg,"i");
if(!reg.test(strVal)){
//验证不通过,就弹出提示
window.alert(myControls[i].getAttribute("tips"));
//返回,并且该表单控件取得焦点
myControls[i].focus();
return false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -