/** regexp-validate-data.js **/ function anyChar(str) { /* Verify at least one nonspace character * or string of characters * Return boolean */ return /\S+/.test(str); }//eof - anyChar function anyWord(str) { /* Verify at least one word character * or string of word characters * a word character is alpha numeric or underscore * Return boolean */ return /\w+/.test(str); }//eof - anyWord function any3letters(str) { /* At least 3 consecutive letters anywhere in str * case insensitive * Return boolean */ return /[a-z]{3}/i.test(str); }//eof - any3letters function any10Char(str) { /* Verify exactly 10 nonspace character * Return string */ if (/^\S{10}$/.test(str)) return "Valid"; else if (/\s/.test(str)) return "Invalid space"; else if (/^\S{11,}$/.test(str)) return "Too long!"; else return "Too short!"; }//eof - any10Char function anyVar10(str) { /* Verify valid JavaScript variable name * of exactly 10 characters * Return string */ if (/^[a-z$_]\w{9}$/i.test(str)) return "Valid"; else if (/^[^a-z$_]/i.test(str)) return "First Char Not Alpha!"; else if (/^[a-z$_]\W/i.test(str)) return "Invalid Character!" else if (/^\w{11,}$/.test(str)) return "Too long!"; else return "Too short!"; }//eof - anyVar10 function anyClass5(str) { /* Verify valid CSS class name of at least 5 characters * Return string */ if (/^[a-z][a-z0-9\-]{4,}$/i.test(str)) return "Valid"; else if (/^[^a-z]/i.test(str)) return "First Char Not Alpha!"; else if (/[^a-z0-9\-]/i.test(str)) return "Invalid Character!"; else return "Too short!"; }//eof - anyClass5 function any4Words(str) { /* Verify at least 4 words can be 1 letter or digit * Return boolean */ return /(\b[a-z0-9]+\b.*){4,}/i.test(str); }//eof - any4Words function three3lettered(str) { /* Verify at least 3 words having at least 3 characters * Return boolean */ return /(\b[a-z0-9]{3,}\b.*){3,}/i.test(str); }//eof - three3lettered function varTest(data) { /* Verify valid JavaScript variable name any length * Return string */ if (/^[a-z$_][\w$]*$/i.test(data)) return "Valid JavaScript Name"; else if (/^[^a-z$_]/i.test(data)) return "Invalid First Character!"; else return "Invalid Character!"; }//eof - varTest function classTest(data) { /* Verify valid CSS class name any length * Return string */ if (/^[a-z][a-z0-9\-]*$/i.test(data)) return "Valid Class Name"; else if (/^[^a-z]/i.test(data)) return "Invalid First Character!"; else return "Invalid Character!"; }//eof - classTest function phpTest(data) { /* Verify valid PHP variable name any length * Return string */ if (/^\$[\w]*$/i.test(data)) return "Valid PHP/Perl Identifier"; else if (/^[^$]/.test(data)) return "Invalid First Character!"; else return "Invalid Character!"; }//eof - phpTest function validate_file(data){ /* Parses filename and extension * for specified extenstions * * Returns Object */ data = data.replace(/^\s*|\s*$/g, ""); //trims string return /^[a-z]\w*\.(asp|html|htm|shtml|php)$/i.test(data) }//eof - validate_file /* Three different email tests return boolean */ function isEmail1(data) { return /^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9]+$/i.test(data); }//eof - isEmail1 function isEmail2(data) { return /^[\w.\-]+@[\w\-]+\.[a-zA-Z0-9]+$/i.test(data); }//eof - isEmail2 function isEmail3(data) { //recommended expression: most complete return /^([\w]+)(\.[\w]+)*@([\w\-]+)(\.[\w]{2,4})(\.[a-z]{2})?$/i.test(data); }//eof - isEmail3 function isSSN1(data) { /* Validate American SSN requiring dashes * Return boolean */ return /^\d{3}-\d{2}-\d{4}$/.test(data); }//eof - isSSN1 function isSSN2(data) { /* Validate American SSN dashes optional * Return boolean */ return /^\d{3}-?\d{2}-?\d{4}$/.test(data); }//eof -isSSN2 function isPhone1(data) { /* Validate American phone number: (123) 456-7890 * requiring parenthesis, space and dash * Return boolean */ return /^\(\d{3}\) \d{3}-\d{4}$/.test(data); }//eof - isPhone1 function isPhone2(data) { /* Validate American phone number: (123) 456-7890 * requiring parenthesis and dash * optional space * Return boolean */ return /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/.test(data); }//eof - isPhone2 function isPhone3(data) { /* Validate American phone number: (123) 456-7890 * optional parenthesis, space and dash * Return boolean */ return /^\(?([1-9]\d{2})(\) ?|[.-])?(\d{3})[.-]?(\d{4})$/.test(data); }//eof - isPhone3 function isValidDate(date_string, format) { /* Validate string user entered as a date in * 1 of 6 formats * m/d/y American month, day, year with 2 or 4 digit year * mm/dd/yy Short American with 2 digit year * mm/dd/yyyy Long American with 4 digit year * y/m/d European year, month, day with 2 or 4 digit year * yy/mm/dd European with 2 digit year * yyyy/mm/dd European with 4 digit year */ var days = [0,31,28,31,30,31,30,31,31,30,31,30,31]; var year, month, day, date_parts = null; var rtrn = false; /* JS Object/Hash table to branch for format */ var decisionTree = { 'm/d/y':{ 're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/, 'month': 1,'day': 2, year: 3 }, 'mm/dd/yy':{ 're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2})$/, 'month': 1,'day': 2, year: 3 }, 'mm/dd/yyyy':{ 're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/, 'month': 1,'day': 2, year: 3 }, 'y/m/d':{ 're':/^(\d{2}|\d{4})[./-](\d{1,2})[./-](\d{1,2})$/, 'month': 2,'day': 3, year: 1 }, 'yy/mm/dd':{ 're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{1,2})$/, 'month': 2,'day': 3, year: 1 }, 'yyyy/mm/dd':{ 're':/^(\d{4})[./-](\d{1,2})[./-](\d{1,2})$/, 'month': 2,'day': 3, year: 1 } }; var test = decisionTree[format]; //Get regexp, etc matching format if (test) { date_parts = date_string.match(test.re); //parse string if (date_parts) { year = date_parts[test.year] - 0; month = date_parts[test.month] - 0; day = date_parts[test.day] - 0; //Get number of days in month -- zero for invalid months test = (month == 2 && isLeapYear() && 29 || days[month] || 0); rtrn = 1 <= day && day <= test; //Check day is in range; false for invalid months } } function isLeapYear() { return (year % 4 != 0 ? false : ( year % 100 != 0? true: ( year % 1000 != 0? false : true))); } return rtrn; }//eof isValidDate /* regexp-validate-numbers.js */ function uInteger(str) { /* Verify unsigned integer * ignoring leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^[0-9]+$/.test(str); }//eof - uInteger function uInteger2(str) { /* Verify unsigned integer * ignoring leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^\d+$/.test(str); }//eof uInteger2 function uInteger3to5(str) { /* Verify unsigned integer 3 to 5 digits * ignoring leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^\d{3,5}$/.test(str); }//eof - uInteger3to5 function sInteger(str) { /* Verify signed integer at least one digit * ignoring leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^[-+]?[0-9]+$/.test(str); }//eof - sInteger function sInteger2(str) { /* Verify signed integer at least one digit * ignoring leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^[-+]?\d+$/.test(str); }//eof - sInteger2 function sInteger3to5(str) { /* Verify signed integer 3 to 5 digits * ignoring leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^[-+]?\d{3,5}$/.test(str); }//eof - sInteger3to5 function isFloat(str) { /* Verify signed float decimal optional * ignoring leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^[-+]?[0-9]+(\.[0-9]+)?$/.test(str); }//eof isFloat function isFloat2(str) { /* Verify signed float decimal optional * ignoring leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^[-+]?\d+(\.\d+)?$/.test(str); }//eof isFloat2 function isFloat3to5d0to3(str) { /* Verify float 3 to 5 digits, decimal max 3 digits * if decimal point must have at least 1 decimal digit * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^[-+]?\d{3,5}(\.\d{1,3})?$/.test(str); }//eof - isFloat3to5d10to3 function isCurrency1(str) { /* Verify formatted currency optional $, no leading zero * reguire comma separator, 2 digit decimal if any * ignore leading and trailing spaces * Return boolean */ str = str.replace(/^\s+|\s+$/g, ''); return /^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(str); }//eof - isCurrency1 function isCurrency2(str) { /* Verify formatted currency optional $, no leading zero * reguire comma separator, 2 digit decimal if any * fillin missing decimal * ignore leading and trailing spaces * Return string */ str = str.replace(/^\s+|\s+$/g, ''); if (/^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{0,2})?$/.test(str) ) { if (/\.[0-9]$/.test(str) ) { //needs trailing zero str += "0"; } else if (/\.$/.test(str)) { //needs both trailing zeroes str += "00"; } else if (!/\.[0-9]{2}$/.test(str) ) { //needs everything str += ".00"; } return str; } else { return "Invalid"; } }//eof - isCurrency2 function isCurrency3(str) { /* Verify formatted currency optional $, no leading zero * reguire comma separator, 2 digit decimal if any * fillin missing decimal * ignore leading and trailing spaces * Return string */ str = str.replace(/^\s+|\s+$/g, ''); if (/^[-+]?\$?[1-9]\d{0,2}(,\d{3})*(\.\d{0,2})?$/.test(str) ) { if (/\.\d$/.test(str)) { str += "0"; } else if (/\.$/.test(str) ) { str += "00"; } else if (!/\.\d{2}$/.test(str) ) { str += ".00"; } return str; } else { return "Invalid"; } }//eof - isCurrency3
详细解决方案
应用JavaScript正则表达式验证
热度:164 发布时间:2012-09-19 13:43:53.0
相关解决方案
- javascript ie6兼容的有关问题
- javascript window open在ie中设立不起作用,求解决
- javascript 字符串拼接效率有关问题
- JavaScript 自动生成图片并合并有关问题
- 不走"<script type='text/javascript'>"标签咋回事
- <script type="javascript/text">的有关问题
- 用servlet+jsp+javascript+jdbc做个简单的办公自动化系统流程,该如何解决
- js 正则表达式 运用变量
- 怎么打开 javascript:SetData(2010,5,10)
- 正则表达式 高手来解解决办法
- javaScript = == ===区别,该怎么解决
- javascript 怎么验证name=xx.xx的radio表单
- form action 和 javascript 的提交問題解决方法
- javascript,该怎么处理
- javascript,该如何处理
- javascript 选中文字 但是保存样式 标签
- 新人求问,J2EE方向,html,css,javascript,vml要学到什么程度?解决思路
- javascript 请求servlet兑现将函数中定义的变量作为参数
- 正则表达式,务必是字母和数字的组合,求解,
- javascript 不懂,该如何处理
- 【java】以上需求怎么写 java 正则表达式
- javascript 不懂解决方法
- JavaScript 大局函数求实例,高分求
- javaScript 里面 如何知道Object 对象的长度
- javascript 函数调用有什么有关问题,请
- 求好手解答 正则表达式 (急)
- 正则表达式
- javascript 中文本框中数字如何比较
- 请问:正则表达式,将一个字符串中出现的空格替换
- javascript IE通过,火狐,google浏览器不过解决思路