来源:ITPOW(www.itpow.com)
通过CExtEdit类,对字符串进行处理,通常用于用户注册、数字填报时检查用户的输入。示例中没有列出文本框,而是直接以字符串代替文本框的值。
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>扩展编辑框</title> </head> <body> <div> <a href="http://www.itpow.com/" target="_blank">ITPOW www.itpow.com 专业探讨【ASP、JavaScript、XHTML+CSS、SQL Server】</a> <hr> </div> <script language="javascript"> <!-- function CExtEdit() { this.minLen = 0; //最小长度 this.maxLen = -1; //最大长度,大于等于 this.minLen 时有效 this.firstCharacter = 1; //0-不限第一个字符,1-字母开头,2-字母下划线开头 this.lcaseLetter = true; //是否允许小写字母 this.ucaseLetter = true; //是否允许大写字母 this.number = true; //是否允许数字 this.underline = true; //是否允许下划线 this.ChckString = ChckString; } //检查长度 //检查第一个字符 //检查字符串 //检查通过返回true;否则返回false function ChckString(str) { if (str.length == 0) { return true; } if ((str.length<this.minLen) || ((this.maxLen>=this.minLen) && (str.length>this.maxLen))) { return false; } var fc = str.substr(0, 1); if (!((((fc>="a" && fc<="z") || (fc>="A" && fc<="Z")) && (this.firstCharacter==1 || this.firstCharacter==2)) || ((fc>="0" && fc<="9") && (this.firstCharacter==2)) || this.firstCharacter==0)) { return false; } var pattern = ""; pattern += (this.lcaseLetter)?"a-z":""; pattern += (this.ucaseLetter)?"A-Z":""; pattern += (this.number)?"0-9":""; pattern += (this.underline)?"_":""; pattern = "[" + pattern + "]{0,}"; var reg = new RegExp(pattern, "g"); if (str.replace(reg, "") == "") { return true; } else { return false; } } //示例 var extEdit = new CExtEdit(); extEdit.firstCharacter = 0; extEdit.minLen = 1; extEdit.maxLen = -1; extEdit.lcaseLetter = true; extEdit.ucaseLetter = true; extEdit.number = true; extEdit.unerline = true; document.write("检查 Aabc 返回:" + extEdit.ChckString("Aabc")); //--> </script> </body> </html>[Ctrl+A 全部选择 提示:你可先修改部分代码,再按运行]
创建于2005年 渝ICP备05010272号-1
电邮:cftea@126.com