JS验证字符串功能
程序员文章站
2023-08-17 13:04:42
js 验证字符串功能实例代码如下所示:
//去除空格
function jstrim(value) {
var reg = /^\s$/;
i...
js 验证字符串功能实例代码如下所示:
//去除空格 function jstrim(value) { var reg = /^\s$/; if (!value) return ""; if (value.length == 0) return value; else { var lp, rp; lp = 0; rp = value.length - 1; while ((value.charat(lp) == " " || (value.charcodeat(lp) < 128 ? (reg.test(value.charat(lp)) == false ? true : false) : false) || value.charat(rp) == " " || (value.charcodeat(rp) < 128 ? (reg.test(value.charat(rp)) == false ? true : false) : false)) && rp >= lp) { if (value.charat(lp) == " " || value.charcodeat(lp) < 128 ? (reg.test(value.charat(lp)) == false ? true : false) : false) lp++; if (value.charat(rp) == " " || value.charcodeat(rp) < 128 ? (reg.test(value.charat(rp)) == false ? true : false) : false) rp--; } if (lp > rp) return ""; else return (value.substring(lp, rp + 1)); } } //字符串长度 function strlength(s) { var s_length = s.length; return s_length; } //手机号码验证函数 string.prototype.trim = function() { var m = this.match(/^\s*(\s+(\s+\s+)*)\s*$/); return (m == null) ? "" : m[1]; } string.prototype.ismobile = function() { return (/^(?:13\d|15\d|18\d|19\d|17\d|14\d)-?\d{5}(\d{3}|\*{3})$/.test(this.trim())); } //*判别电话号码,手机号是否有效 //*返回 true:false //*/ function checkphone(phone) { //验证电话号码手机号码,包含153,159号段 if (phone==""){ alert("电话号码不能为空!"); return false; } if (phone != ""){ var p1 = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/; var me = false; if (p1.test(phone)) me=true; if (!me){ //alert('对不起,您输入的电话号码有错误。区号和电话号码之间请用-分割'); return false; } } return true; } //邮箱地址验证函数 function checkmail(mail) { // var strr; // re = /(\w+@\w+\.\w+)(\.{0,1}\w*)(\.{0,1}\w*)/i; // re.exec(mail); // if (regexp.$3 != "" && regexp.$3 != "." && regexp.$2 != ".") // strr = regexp.$1 + regexp.$2 + regexp.$3 // else { // if (regexp.$2 != "" && regexp.$2 != ".") // strr = regexp.$1 + regexp.$2 // else // strr = regexp.$1 // } // if (strr != mail) { return false } else { return true } // return true; var remail = /^([a-za-z0-9_\.\-])+\@(([a-za-z0-9\-])+\.)+([a-za-z0-9]{2,4})+$/; return mail.match(remail); } //判断数字 function checknumber(n) { var checkok = "0123456789-."; var allvalid = true; var decpoints = 0; var allnum = ""; for (i = 0; i < n.length; i++) { ch = n.charat(i); for (j = 0; j < checkok.length; j++) if (ch == checkok.charat(j)) break; if (j == checkok.length) { allvalid = false; break; } if (ch == ".") { allnum += "."; decpoints++; } else allnum += ch; } if (!allvalid) return false; if (decpoints > 1) return false; return true; } //判断数字 function checkn(n) { var checkok = "0123456789"; var allvalid = true; var decpoints = 0; var allnum = ""; for (i = 0; i < n.length; i++) { ch = n.charat(i); for (j = 0; j < checkok.length; j++) if (ch == checkok.charat(j)) break; if (j == checkok.length) { allvalid = false; break; } if (ch == ".") { allnum += "."; decpoints++; } else allnum += ch; } if (!allvalid) return false; if (decpoints > 1) return false; return true; } var g_miny = 1601; var g_maxy = 4500; var rgmc = new array(12); rgmc[0] = 31; rgmc[1] = 28; rgmc[2] = 31; rgmc[3] = 30; rgmc[4] = 31; rgmc[5] = 30; rgmc[6] = 31; rgmc[7] = 31; rgmc[8] = 30; rgmc[9] = 31; rgmc[10] = 30; rgmc[11] = 31; function isdigit(c) { return (c >= '0' && c <= '9'); } function getmonthcount(m, y) { var c = rgmc[m - 1]; if ((2 == m) && isleapyear(y)) c++; return c; } function atoi(s) { var t = 0; for (var i = 0; i < s.length; i++) { var c = s.charat(i); if (!isdigit(c)) return t; else t = t * 10 + (c - '0'); } return t; } //判断日期 function checkdate(t) { var l = t.length; if (0 == l) return false; var csp = '\0'; var ssp1 = ''; var ssp2 = ''; for (var i = 0; i < t.length; i++) { var c = t.charat(i); if (c == ' ' || isdigit(c)) continue; else if (csp == '\0' && (c == '/' || c == '-' || c == '.')) { csp = c; ssp1 = t.substring(i + 1, l); } else if (c == csp) ssp2 = t.substring(i + 1, l); else if (c != csp) return false; } if (0 == ssp1.length) return false; var m; var d; var y; if (0 == ssp2.length) return false; m = atoi(ssp1); d = atoi(ssp2); y = atoi(t); if (y < 100) { y = 1900 + y; while (y < g_ylow) y = y + 100; } if (y < g_miny || y > g_maxy || m < 1 || m > 12) return false; if (d < 1 || d > getmonthcount(m, y)) return false; g_month = m; g_day = d; g_year = y; return true; } //验证字符串必须为数字字母组合 function isnumandstr(elem){ var str = elem.value; var regexpuperstr=/[a-z]+/; var reexplowerstr=/[a-z]+/; var regexpnum=/\d+/; var uperstrflag = regexpuperstr.test(str); var lowerstrflag = reexplowerstr.test(str); var numflag = regexpnum.test(str); if((uperstrflag&&lowerstrflag)||(lowerstrflag&&numflag)||(uperstrflag&&numflag)) return true; else return false; } //验证字符串必须为数字跟字母的组合 function checkpsw(str) { if (str != "") { var p1 = /(?!^[0-9]*$)(?!^[a-za-z]*$)^([a-za-z0-9]{2,})$/; var me = false; if (p1.test(str)) me = true; if (!me) { return false; } } return true; } //验证字符串必须为数字跟字母的组合 function checklen(str) { if (str.length < 8 ) { return false; } return true; } //单价 function isprice(v) { var mreg = /^[0-9]{1,10}(\.[0-9]{1,3})?$/; if (mreg.test(v)) { return true; } else { return false; } }
以上所述是小编给大家介绍的js验证字符串功能,希望对大家有所帮助
上一篇: jquery实现焦点轮播效果
下一篇: 如何管理好你的痛风病 饮食指南拿走不谢