- /** 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
2012年3月11日星期日
使用JavaScript正则表达式验证数据
订阅:
博文评论 (Atom)
没有评论:
发表评论