JS验证字符串功能
js 验证字符串功能实例代码如下所示:
创新互联建站主营建湖网站建设的网络公司,主营网站建设方案,app软件定制开发,建湖h5小程序定制开发搭建,建湖网站营销推广欢迎建湖等地区企业咨询
//去除空格 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验证字符串功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对创新互联网站的支持!
本文标题:JS验证字符串功能
当前网址:http://ybzwz.com/article/gihogg.html