JavaScript正则表达式检验与递归函数实际应用
//校验是否全由数字组成function isDigit(s){var patrn=/^[0-9]{1,20}$/;if (!patrn.exec(s)) return falsereturn true}//校验登录名:只能输入5-20个以字母开头、可带数字、“_”、“.”的字串function isRegisterUserName(s){var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;if (!patrn.exec(s)) return falsereturn true}//校验用户姓名:只能输入1-30个以字母开头的字串function isTrueName(s){var patrn=/^[a-zA-Z]{1,30}$/;if (!patrn.exec(s)) return falsereturn true}//校验密码:只能输入6-20个字母、数字、下划线function isPasswd(s){var patrn=/^(\w){6,20}$/;if (!patrn.exec(s)) return falsereturn true}//校验普通电话、传真号码:可以“+”开头,除数字外,可含有“-”function isTel(s){//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;if (!patrn.exec(s)) return falsereturn true}//校验手机号码:必须以数字开头,除数字外,可含有“-”function isMobil(s){var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;if (!patrn.exec(s)) return falsereturn true}//校验邮政编码function isPostalCode(s){//var patrn=/^[a-zA-Z0-9]{3,12}$/;var patrn=/^[a-zA-Z0-9 ]{3,12}$/;if (!patrn.exec(s)) return falsereturn true}//校验搜索关键字function isSearch(s){var patrn=/^[^`~!@#$%^&*()+=|\\\][\]\{\}:;\'\,.<>/?]{1}[^`~!@$%^&()+=|\\\][\]\{\}:;\'\,.<>?]{0,19}$/;if (!patrn.exec(s)) return falsereturn true}function isIP(s) //by zergling{var patrn=/^[0-9.]{1,20}$/;if (!patrn.exec(s)) return falsereturn true} * FUNCTION: isBetween* PARAMETERS: val AS any value* lo AS Lower limit to check* hi AS Higher limit to check* CALLS: NOTHING* RETURNS: TRUE if val is between lo and hi both inclusive, otherwise false. function isBetween (val, lo, hi) {if ((val < lo) || (val > hi)) { return(false); }else { return(true); }} * FUNCTION: isDate checks a valid date* PARAMETERS: theStr AS String* CALLS: isBetween, isInt* RETURNS: TRUE if theStr is a valid date otherwise false. function isDate (theStr) {var the1st = theStr.indexOf('-');var the2nd = theStr.lastIndexOf('-');if (the1st == the2nd) { return(false); }else {var y = theStr.substring(0,the1st);var m = theStr.substring(the1st+1,the2nd);var d = theStr.substring(the2nd+1,theStr.length);var maxDays = 31;if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {return(false); }else if (y.length < 4) { return(false); }else if (!isBetween (m, 1, 12)) { return(false); }else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;else if (m==2) {if (y % 4 > 0) maxDays = 28;else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;else maxDays = 29;}if (isBetween(d, 1, maxDays) == false) { return(false); }else { return(true); }}} * FUNCTION: isEuDate checks a valid date in British format* PARAMETERS: theStr AS String* CALLS: isBetween, isInt* RETURNS: TRUE if theStr is a valid date otherwise false. function isEuDate (theStr) {if (isBetween(theStr.length, 8, 10) == false) { return(false); }else {var the1st = theStr.indexOf('/');var the2nd = theStr.lastIndexOf('/');if (the1st == the2nd) { return(false); }else {var m = theStr.substring(the1st+1,the2nd);var d = theStr.substring(0,the1st);var y = theStr.substring(the2nd+1,theStr.length);var maxDays = 31;if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {return(false); }else if (y.length < 4) { return(false); }else if (isBetween (m, 1, 12) == false) { return(false); }else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;else if (m==2) {if (y % 4 > 0) maxDays = 28;else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;else maxDays = 29;}if (isBetween(d, 1, maxDays) == false) { return(false); }else { return(true); }}}} * FUNCTION: Compare Date! Which is the latest!* PARAMETERS: lessDate,moreDate AS String* CALLS: isDate,isBetween* RETURNS: TRUE if lessDate<moreDate function isComdate (lessDate , moreDate){if (!isDate(lessDate)) { return(false);}if (!isDate(moreDate)) { return(false);}var less1st = lessDate.indexOf('-');var less2nd = lessDate.lastIndexOf('-');var more1st = moreDate.indexOf('-');var more2nd = moreDate.lastIndexOf('-');var lessy = lessDate.substring(0,less1st);var lessm = lessDate.substring(less1st+1,less2nd);var lessd = lessDate.substring(less2nd+1,lessDate.length);var morey = moreDate.substring(0,more1st);var morem = moreDate.substring(more1st+1,more2nd);var mored = moreDate.substring(more2nd+1,moreDate.length);var Date1 = new Date(lessy,lessm,lessd); var Date2 = new Date(morey,morem,mored); if (Date1>Date2) { return(false);}return(true);} * FUNCTION isEmpty checks if the parameter is empty or null* PARAMETER str AS String function isEmpty (str) {if ((str==null)||(str.length==0)) return true;else return(false);} * FUNCTION: isInt* PARAMETER: theStr AS String * RETURNS: TRUE if the passed parameter is an integer, otherwise FALSE* CALLS: isDigit function isInt (theStr) {var flag = true;if (isEmpty(theStr)) { flag=false; }else{ for (var i=0; i<theStr.length; i++) {if (isDigit(theStr.substring(i,i+1)) == false) {flag = false; break;}}}return(flag);} * FUNCTION: isReal* PARAMETER: heStr AS String decLen AS Integer (how many digits after period)* RETURNS: TRUE if theStr is a float, otherwise FALSE* CALLS: isInt function isReal (theStr, decLen) {var dot1st = theStr.indexOf('.');var dot2nd = theStr.lastIndexOf('.');var OK = true;if (isEmpty(theStr)) return false;if (dot1st == -1) {if (!isInt(theStr)) return(false);else return(true);}else if (dot1st != dot2nd) return (false);else if (dot1st==0) return (false);else {var intPart = theStr.substring(0, dot1st);var decPart = theStr.substring(dot2nd+1);if (decPart.length > decLen) return(false);else if (!isInt(intPart) || !isInt(decPart)) return (false);else if (isEmpty(decPart)) return (false);else return(true);}} * FUNCTION: isEmail* PARAMETER: String (Email Address)* RETURNS: TRUE if the String is a valid Email address* FALSE if the passed string is not a valid Email Address* EMAIL FORMAT: AnyName@EmailServer e.g; webmaster@hotmail.com* @ sign can appear only once in the email address. function isEmail (theStr) {var atIndex = theStr.indexOf('@');var dotIndex = theStr.indexOf('.', atIndex);var flag = true;theSub = theStr.substring(0, dotIndex+1)if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length)) { return(false); }else { return(true); }} * FUNCTION: newWindow* PARAMETERS: doc -> Document to open in the new windowhite -> Height of the new windowwide -> Width of the new windowbars -> 1-Scroll bars = YES 0-Scroll Bars = NOresize -> 1-Resizable = YES 0-Resizable = NO* CALLS: NONE* RETURNS: New window instance function newWindow (doc, hite, wide, bars, resize) {var winNew="_blank";var opt="toolbar=0,location=0,directories=0,status=0,menubar=0,";opt+=("scrollbars="+bars+",");opt+=("resizable="+resize+",");opt+=("width="+wide+",");opt+=("height="+hite);winHandle=window.open(doc,winNew,opt);return;} * FUNCTION: DecimalFormat* PARAMETERS: paramValue -> Field value* CALLS: NONE* RETURNS: Formated string function DecimalFormat (paramValue) {var intPart = parseInt(paramValue);var decPart =parseFloat(paramValue) - intPart;str = "";if ((decPart == 0) || (decPart == null)) str += (intPart + ".00");else str += (intPart + decPart);return (str);}"^[0-9]*[1-9][0-9]*$" //正整数
"^((-\\d+)|(0+))$" //非正整数(负整数 + 0)
"^-[0-9]*[1-9][0-9]*$" //负整数
"^-?\\d+$" //整数
"^\\d+(\\.\\d+)?$" //非负浮点数(正浮点数 + 0)
"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$" //正浮点数
"^((-\\d+(\\.\\d+)?)|(0+(\\.0+)?))$" //非正浮点数(负浮点数 + 0)
"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$" //负浮点数
"^(-?\\d+)(\\.\\d+)?$" //浮点数
"^[A-Za-z]+$" //由26个英文字母组成的字符串
"^[A-Z]+$" //由26个英文字母的大写组成的字符串
"^[a-z]+$" //由26个英文字母的小写组成的字符串
"^[A-Za-z0-9]+$" //由数字和26个英文字母组成的字符串
"^\\w+$" //由数字、26个英文字母或者下划线组成的字符串
"^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$" //email地址
"^[a-zA-z]+://(\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$" //url
JavaScript正则表达式检验与递归函数实际应用的更多相关文章
- JavaScript -- 正则表达式 检验表单提交的内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JavaScript正则表达式检验手机号码、邮箱、ip地址等
1)检测IP地址的正则表达式 ((2 [0-4] \d | 25[0-5] | [01]?\d\d?) \.){3}(2 [0-4] \d | 25[0-5] | [01]?\d\d?) 2 [0-4 ...
- javascript正则表达式 - 学习笔记
JavaScript 正则表达式 学习笔记 标签(空格分隔): 基础 JavaScript 正则表达式是用于匹配字符串中字符组合的模式.在javascript中,正则表达式也是对象.这些模式被用于Re ...
- JavaScript正则表达式,你真的知道?
一.前言 粗浅的编写正则表达式,是造成性能瓶颈的主要原因.如下: var reg1 = /(A+A+)+B/; var reg2 = /AA+B/; 上述两个正则表达式,匹配效果是一样的,但是,效率就 ...
- 【JS】javascript 正则表达式 大全 总结
javascript 正则表达式 大全 总结 参考整理了一些javascript正则表达式 目的一:自我复习归纳总结 目的二:共享方便大家搜索 微信:wixf150 验证数字:^[0-9]*$ 验证n ...
- 理清JavaScript正则表达式--上篇
在JavaScript中,正则表达式由RegExp对象表示.RegExp对象呢,又可以通过直接量和构造函数RegExp两种方式创建,分别如下: //直接量 var re = /pattern/[g | ...
- 理清JavaScript正则表达式--下篇
紧接:"理清JavaScript正则表达式--上篇". 正则在String类中的应用 类String支持四种利用正则表达式的方法.分别是search.replace.match和s ...
- Test Regular Expressions Online with RegExr免费的正则表达式检验网站
免费的正则表达式检验网站: http://www.regexr.com
- JavaScript正则表达式详解(一)正则表达式入门
JavaScript正则表达式是很多JavaScript开发人员比较头疼的事情,也很多人不愿意学习,只是必要的时候上网查一下就可以啦~本文中详细的把JavaScript正则表达式的用法进行了列表,希望 ...
随机推荐
- win7 点IE浏览器无法打开
现象:win7系统 打开未响应, 装其他浏览器可以使用,但很慢,且有密码框输入不了 处理: WIN7删除添加程序中,选打开或关闭windows功能,其中就有IE11的勾选了, 然后清除勾选,即会出现提 ...
- php接口加密
在实际开发中PHP接口开发是PHP常见的技术,可以通过接口进行一些操作,可以访问接口获取数据,给app提供数据接口等,但是如果接口没有加密解密的过程,很容易发生盗用和不安全的操作. 下边简单介绍一下A ...
- java使用dom4j对XML进行CURD操作
要使用dom4j必须导入两个jar包: dom4j-1.6.1.jar jaxen-1.1-beta-6.jar 使用dom4j新建一个XML文件: /** * 利用dom4j完成新增一个xml文件 ...
- python cookbook第三版学习笔记十:类和对象(一)
类和对象: 我们经常会对打印一个对象来得到对象的某些信息. class pair: def __init__(self,x,y): self.x=x self. ...
- ASP.NET Core 开源论坛项目 NETCoreBBS
ASP.NET Core 轻量化开源论坛项目,ASP.NET Core Light forum NETCoreBBS 采用 ASP.NET Core + EF Core Sqlite + Bootst ...
- classloader加载的双亲委托模式
要深入了解ClassLoader,首先就要知道ClassLoader是用来干什么的,顾名思义,它就是用来加载Class文件到JVM,以供程序使用 的.我们知道,java程序可以动态加载类定义,而这个动 ...
- 干货~powershell与bash和docker在项目中怎么用
回到目录 这个标题够直接了吧,够坦诚了吧,也许你在项目里这三个东西都没有用到,但这三个东西在未来的两年里将成为最HOT的技术,它们不是什么框架,也不是什么设计模式,而是做为程序和环境快速部署而设计出来 ...
- js实现日期显示的一些操作
1.js获取当前日期(yyyy-mm-dd) 以下代码是获取到的当前日期: var myDate = new Date(); var year = myDate.getFullYear(); //获取 ...
- Python_网络爬虫(新浪新闻抓取)
爬取前的准备: BeautifulSoup的导入:pip install BeautifulSoup4 requests的导入:pip install requests 下载jupyter noteb ...
- 简单设置android启动画面
1.新建Activity,以及layout文件夹里的xml文件2.将新建Activity在AndroidManifest中设为默认Activity,并且添加:android:theme="@ ...