2.5 中文/英文/数字/邮件地址合法性判断 <SCRIPT LANGUAGE="javascript"> <!-- function isEnglish(name) //英文值检测 { if(name.length == 0) return false; for(i = 0; i < name.length; i++) { if(name.charCodeAt(i) > 128) return false; } return true; } function isChinese(name) //中文值检测 { if(name.length == 0) return false; for(i = 0; i < name.length; i++) { if(name.charCodeAt(i) > 128) return true; } return false; } function isMail(name) // E-mail值检测 { if(! isEnglish(name)) return false; i = name.indexOf(" at "); j = name dot lastIndexOf(" at "); if(i == -1) return false; if(i != j) return false; if(i == name dot length) return false; return true; } function isNumber(name) //数值检测 { if(name.length == 0) return false; for(i = 0; i < name.length; i++) { if(name.charAt(i) < "0" || name.charAt(i) > "9") return false; } return true; } function CheckForm() { if(! isMail(form.Email.value)) { alert("您的电子邮件不合法!"); form.Email.focus(); return false; } if(! isEnglish(form.name.value)) { alert("英文名不合法!"); form.name.focus(); return false; } if(! isChinese(form.cnname.value)) { alert("中文名不合法!"); form.cnname.focus(); return false; } if(! isNumber(form.PublicZipCode.value)) { alert("邮政编码不合法!"); form.PublicZipCode.focus(); return false; } return true; } //--> </SCRIPT> 2.6 限定表单项不能输入的字符 <script language="javascript"> <!-- function contain(str,charset)// 字符串包含测试函数 { var i; for(i=0;i<charset.length;i++) if(str.indexOf(charset.charAt(i))>=0) return true; return false; } function CheckForm() { if ((contain(document.form.NAME.value, "%\(\)><")) || (contain(document.form.MESSAGE.value, "%\(\)><"))) { alert("输入了非法字符"); document.form.NAME.focus(); return false; } return true; } //--> </script> 1. 检查一段字符串是否全由数字组成 --------------------------------------- <script language="Javascript"><!-- function checkNum(str){return str.match(/\D/)==null} alert(checkNum("1232142141")) alert(checkNum("123214214a1")) // --></script> 2. 怎么判断是否是字符 --------------------------------------- if (/[^\x00-\xff]/g.test(s)) alert("含有汉字"); else alert("全是字符"); 3. 怎么判断是否含有汉字 --------------------------------------- if (escape(str).indexOf("%u")!=-1) alert("含有汉字"); else alert("全是字符"); 4. 邮箱格式验证 --------------------------------------- //函数名:chkemail //功能介绍:检查是否为Email Address //参数说明:要检查的字符串 //返回值:0:不是 1:是 function chkemail(a) { var i=a.length; var temp = a.indexOf('@'); var tempd = a.indexOf('.'); if (temp > 1) { if ((i-temp) > 3){ if ((i-tempd)>0){ return 1; } } } return 0; }
相关资源:js简单的注册表单密码验证代码