2012年3月11日星期日

使用JavaScript正则表达式验证数据


  1. /** regexp-validate-data.js **/  
  2. function anyChar(str) {  
  3.     /* Verify at least one nonspace character 
  4.      *    or string of characters 
  5.      * Return boolean 
  6.      */  
  7.     return /\S+/.test(str);  
  8. }//eof - anyChar  
  9.   
  10. function anyWord(str) {  
  11.     /* Verify at least one word character 
  12.      *        or string of word characters 
  13.      *        a word character is alpha numeric or underscore 
  14.      * Return boolean 
  15.      */  
  16.     return /\w+/.test(str);  
  17. }//eof - anyWord  
  18.   
  19. function any3letters(str) {  
  20.     /* At least 3 consecutive letters anywhere in str 
  21.      *    case insensitive 
  22.      * Return boolean 
  23.      */  
  24.     return /[a-z]{3}/i.test(str);  
  25. }//eof - any3letters  
  26.   
  27. function any10Char(str) {  
  28.     /* Verify exactly 10 nonspace character 
  29.      * Return string 
  30.      */  
  31.     if (/^\S{10}$/.test(str))  
  32.         return "Valid";  
  33.     else if (/\s/.test(str))  
  34.         return "Invalid space";  
  35.     else if (/^\S{11,}$/.test(str))  
  36.         return "Too long!";  
  37.     else  
  38.         return "Too short!";  
  39. }//eof - any10Char  
  40.   
  41. function anyVar10(str) {  
  42.     /* Verify valid JavaScript variable name 
  43.      *     of exactly 10 characters 
  44.      * Return string 
  45.      */  
  46.     if (/^[a-z$_]\w{9}$/i.test(str))  
  47.         return "Valid";  
  48.     else if (/^[^a-z$_]/i.test(str))  
  49.         return "First Char Not Alpha!";  
  50.     else if (/^[a-z$_]\W/i.test(str))  
  51.         return "Invalid Character!"  
  52.     else if (/^\w{11,}$/.test(str))  
  53.         return "Too long!";  
  54.     else  
  55.         return "Too short!";  
  56. }//eof - anyVar10  
  57.   
  58. function anyClass5(str) {  
  59.     /* Verify valid CSS class name of at least 5 characters 
  60.      * Return string 
  61.      */  
  62.     if (/^[a-z][a-z0-9\-]{4,}$/i.test(str))  
  63.         return "Valid";  
  64.     else if (/^[^a-z]/i.test(str))  
  65.         return "First Char Not Alpha!";  
  66.     else if (/[^a-z0-9\-]/i.test(str))  
  67.         return "Invalid Character!";  
  68.     else  
  69.         return "Too short!";  
  70. }//eof - anyClass5  
  71.   
  72. function any4Words(str) {  
  73.     /* Verify at least 4 words can be 1 letter or digit 
  74.      * Return boolean 
  75.      */  
  76.     return /(\b[a-z0-9]+\b.*){4,}/i.test(str);  
  77. }//eof - any4Words  
  78.   
  79. function three3lettered(str) {  
  80.     /* Verify at least 3 words having at least 3 characters 
  81.      * Return boolean 
  82.      */  
  83.     return /(\b[a-z0-9]{3,}\b.*){3,}/i.test(str);  
  84. }//eof - three3lettered  
  85.   
  86. function varTest(data) {  
  87.     /* Verify valid JavaScript variable name any length 
  88.      * Return string 
  89.      */  
  90.     if (/^[a-z$_][\w$]*$/i.test(data))  
  91.         return "Valid JavaScript Name";  
  92.     else if (/^[^a-z$_]/i.test(data))  
  93.         return "Invalid First Character!";  
  94.     else  
  95.         return "Invalid Character!";  
  96. }//eof - varTest  
  97.   
  98. function classTest(data) {  
  99.     /* Verify valid CSS class name any length 
  100.      * Return string 
  101.      */  
  102.     if (/^[a-z][a-z0-9\-]*$/i.test(data))  
  103.         return "Valid Class Name";  
  104.     else if (/^[^a-z]/i.test(data))  
  105.         return "Invalid First Character!";  
  106.     else  
  107.         return "Invalid Character!";  
  108. }//eof - classTest  
  109.   
  110. function phpTest(data) {  
  111.     /* Verify valid PHP variable name any length 
  112.      * Return string 
  113.      */  
  114.     if (/^\$[\w]*$/i.test(data))  
  115.         return "Valid PHP/Perl Identifier";  
  116.     else if (/^[^$]/.test(data))  
  117.         return "Invalid First Character!";  
  118.     else  
  119.         return "Invalid Character!";  
  120. }//eof - phpTest  
  121.   
  122. function validate_file(data){  
  123.     /* Parses filename and extension 
  124.      * for specified extenstions 
  125.      * 
  126.      * Returns Object 
  127.      */  
  128.     data = data.replace(/^\s*|\s*$/g, ""); //trims string  
  129.   
  130.     return /^[a-z]\w*\.(asp|html|htm|shtml|php)$/i.test(data)  
  131. }//eof - validate_file  
  132.   
  133. /* Three different email tests return boolean */  
  134. function isEmail1(data) {  
  135.     return /^[a-zA-Z0-9_\-.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9]+$/i.test(data);  
  136. }//eof - isEmail1  
  137.   
  138. function isEmail2(data) {  
  139.     return /^[\w.\-]+@[\w\-]+\.[a-zA-Z0-9]+$/i.test(data);  
  140. }//eof - isEmail2  
  141.   
  142. function isEmail3(data) {  
  143.     //recommended expression: most complete  
  144.     return /^([\w]+)(\.[\w]+)*@([\w\-]+)(\.[\w]{2,4})(\.[a-z]{2})?$/i.test(data);  
  145. }//eof - isEmail3  
  146.   
  147. function isSSN1(data) {  
  148.     /* Validate American SSN requiring dashes 
  149.      * Return boolean 
  150.      */  
  151.     return /^\d{3}-\d{2}-\d{4}$/.test(data);  
  152. }//eof - isSSN1  
  153.   
  154. function isSSN2(data) {  
  155.     /* Validate American SSN dashes optional 
  156.      * Return boolean 
  157.      */  
  158.     return /^\d{3}-?\d{2}-?\d{4}$/.test(data);  
  159. }//eof -isSSN2  
  160.   
  161. function isPhone1(data) {  
  162.     /* Validate American phone number: (123) 456-7890 
  163.      *    requiring parenthesis, space and dash 
  164.      * Return boolean 
  165.      */  
  166.     return /^\(\d{3}\) \d{3}-\d{4}$/.test(data);  
  167. }//eof - isPhone1  
  168.   
  169. function isPhone2(data) {  
  170.     /* Validate American phone number: (123) 456-7890 
  171.      *    requiring parenthesis and dash 
  172.      *    optional space 
  173.      * Return boolean 
  174.      */  
  175.     return /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/.test(data);  
  176. }//eof - isPhone2  
  177.   
  178. function isPhone3(data) {  
  179.     /* Validate American phone number: (123) 456-7890 
  180.      *    optional parenthesis, space and dash 
  181.      * Return boolean 
  182.      */  
  183.     return /^\(?([1-9]\d{2})(\) ?|[.-])?(\d{3})[.-]?(\d{4})$/.test(data);  
  184. }//eof - isPhone3  
  185.   
  186. function isValidDate(date_string, format) {  
  187.     /* Validate string user entered as a date in  
  188.      * 1 of 6 formats 
  189.      *  m/d/y       American month, day, year with 2 or 4 digit year 
  190.      *  mm/dd/yy    Short American with 2 digit year 
  191.      *  mm/dd/yyyy  Long American with 4 digit year 
  192.      *  y/m/d       European year, month, day with 2 or 4 digit year 
  193.      *  yy/mm/dd    European with 2 digit year 
  194.      *  yyyy/mm/dd  European with 4 digit year 
  195.      */  
  196.     var days = [0,31,28,31,30,31,30,31,31,30,31,30,31];  
  197.     var year, month, day, date_parts = null;  
  198.     var rtrn = false;  
  199.   
  200.     /* JS Object/Hash table to branch for format */  
  201.     var decisionTree = {  
  202.         'm/d/y':{  
  203.             're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2}|\d{4})$/,  
  204.             'month'1,'day'2, year: 3  
  205.         },  
  206.         'mm/dd/yy':{  
  207.             're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{2})$/,  
  208.             'month'1,'day'2, year: 3  
  209.         },  
  210.         'mm/dd/yyyy':{  
  211.             're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{4})$/,  
  212.             'month'1,'day'2, year: 3  
  213.         },  
  214.         'y/m/d':{  
  215.             're':/^(\d{2}|\d{4})[./-](\d{1,2})[./-](\d{1,2})$/,  
  216.             'month'2,'day'3, year: 1  
  217.         },  
  218.         'yy/mm/dd':{  
  219.             're':/^(\d{1,2})[./-](\d{1,2})[./-](\d{1,2})$/,  
  220.             'month'2,'day'3, year: 1  
  221.         },  
  222.         'yyyy/mm/dd':{  
  223.                 're':/^(\d{4})[./-](\d{1,2})[./-](\d{1,2})$/,  
  224.                 'month'2,'day'3, year: 1  
  225.         }  
  226.     };  
  227.   
  228.     var test = decisionTree[format]; //Get regexp, etc matching format  
  229.     if (test) {  
  230.         date_parts = date_string.match(test.re); //parse string  
  231.         if (date_parts) {  
  232.             year = date_parts[test.year] - 0;  
  233.             month = date_parts[test.month] - 0;  
  234.             day = date_parts[test.day] - 0;  
  235.   
  236.             //Get number of days in month -- zero for invalid months  
  237.             test = (month == 2 &&   
  238.                     isLeapYear() &&   
  239.                     29 || days[month] || 0);  
  240.   
  241.             rtrn = 1 <= day && day <= test; //Check day is in range; false for invalid months  
  242.         }  
  243.     }  
  244.   
  245.     function isLeapYear() {  
  246.         return (year % 4 != 0 ? false :   
  247.             ( year % 100 != 0true:   
  248.             ( year % 1000 != 0false : true)));  
  249.     }  
  250.     return rtrn;  
  251. }//eof isValidDate  
  252.   
  253. /* regexp-validate-numbers.js */  
  254. function uInteger(str) {  
  255.     /* Verify unsigned integer 
  256.      *        ignoring leading and trailing spaces 
  257.      * Return boolean 
  258.      */  
  259.     str = str.replace(/^\s+|\s+$/g, '');  
  260.     return /^[0-9]+$/.test(str);  
  261. }//eof - uInteger  
  262.   
  263. function uInteger2(str) {  
  264.     /* Verify unsigned integer 
  265.      *        ignoring leading and trailing spaces 
  266.      * Return boolean 
  267.      */  
  268.     str = str.replace(/^\s+|\s+$/g, '');  
  269.     return /^\d+$/.test(str);  
  270. }//eof uInteger2  
  271.   
  272. function uInteger3to5(str) {  
  273.     /* Verify unsigned integer 3 to 5 digits 
  274.      *        ignoring leading and trailing spaces 
  275.      * Return boolean 
  276.      */  
  277.     str = str.replace(/^\s+|\s+$/g, '');  
  278.     return /^\d{3,5}$/.test(str);  
  279. }//eof - uInteger3to5  
  280.   
  281. function sInteger(str) {  
  282.     /* Verify signed integer at least one digit 
  283.      *        ignoring leading and trailing spaces 
  284.      * Return boolean 
  285.      */  
  286.     str = str.replace(/^\s+|\s+$/g, '');  
  287.     return /^[-+]?[0-9]+$/.test(str);  
  288. }//eof - sInteger  
  289.   
  290. function sInteger2(str) {  
  291.     /* Verify signed integer at least one digit 
  292.      *        ignoring leading and trailing spaces 
  293.      * Return boolean 
  294.      */  
  295.     str = str.replace(/^\s+|\s+$/g, '');  
  296.     return /^[-+]?\d+$/.test(str);  
  297. }//eof - sInteger2  
  298.   
  299. function sInteger3to5(str) {  
  300.     /* Verify signed integer 3 to 5 digits 
  301.      *        ignoring leading and trailing spaces 
  302.      * Return boolean 
  303.      */  
  304.     str = str.replace(/^\s+|\s+$/g, '');  
  305.     return /^[-+]?\d{3,5}$/.test(str);  
  306. }//eof - sInteger3to5  
  307.   
  308. function isFloat(str) {  
  309.     /* Verify signed float decimal optional 
  310.      *    ignoring leading and trailing spaces 
  311.      * Return boolean 
  312.      */  
  313.     str = str.replace(/^\s+|\s+$/g, '');  
  314.     return /^[-+]?[0-9]+(\.[0-9]+)?$/.test(str);  
  315. }//eof isFloat  
  316.   
  317. function isFloat2(str) {  
  318.     /* Verify signed float decimal optional 
  319.      *    ignoring leading and trailing spaces 
  320.      * Return boolean 
  321.      */  
  322.     str = str.replace(/^\s+|\s+$/g, '');  
  323.     return /^[-+]?\d+(\.\d+)?$/.test(str);  
  324. }//eof isFloat2  
  325.   
  326. function isFloat3to5d0to3(str) {  
  327.     /* Verify float 3 to 5 digits, decimal max 3 digits 
  328.      *    if decimal point must have at least 1 decimal digit 
  329.      * Return boolean 
  330.      */  
  331.     str = str.replace(/^\s+|\s+$/g, '');  
  332.     return /^[-+]?\d{3,5}(\.\d{1,3})?$/.test(str);  
  333. }//eof - isFloat3to5d10to3  
  334.   
  335. function isCurrency1(str) {  
  336.     /* Verify formatted currency optional $, no leading zero 
  337.      *    reguire comma separator, 2 digit decimal if any 
  338.      *    ignore leading and trailing spaces 
  339.      * Return boolean 
  340.      */  
  341.     str = str.replace(/^\s+|\s+$/g, '');  
  342.     return /^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(str);  
  343. }//eof - isCurrency1  
  344.   
  345. function isCurrency2(str) {  
  346.     /*  Verify formatted currency optional $, no leading zero 
  347.      *    reguire comma separator, 2 digit decimal if any 
  348.      *    fillin missing decimal 
  349.      *    ignore leading and trailing spaces 
  350.      * Return string 
  351.      */  
  352.     str = str.replace(/^\s+|\s+$/g, '');  
  353.     if (/^\$?[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{0,2})?$/.test(str) ) {  
  354.   
  355.         if (/\.[0-9]$/.test(str) ) {  
  356.             //needs trailing zero  
  357.             str += "0";  
  358.         }  
  359.         else if (/\.$/.test(str)) {  
  360.             //needs both trailing zeroes  
  361.             str += "00";  
  362.         }  
  363.         else if (!/\.[0-9]{2}$/.test(str) ) {  
  364.             //needs everything  
  365.             str += ".00";  
  366.         }  
  367.         return str;  
  368.     }  
  369.     else {  
  370.         return "Invalid";  
  371.     }  
  372. }//eof - isCurrency2  
  373.   
  374. function isCurrency3(str) {  
  375.     /* Verify formatted currency optional $, no leading zero 
  376.      *    reguire comma separator, 2 digit decimal if any 
  377.      *    fillin missing decimal 
  378.      *    ignore leading and trailing spaces 
  379.      * Return string 
  380.      */  
  381.     str = str.replace(/^\s+|\s+$/g, '');  
  382.   
  383.     if (/^[-+]?\$?[1-9]\d{0,2}(,\d{3})*(\.\d{0,2})?$/.test(str) ) {  
  384.   
  385.         if (/\.\d$/.test(str)) {  
  386.             str += "0";  
  387.         }  
  388.         else if (/\.$/.test(str) ) {  
  389.             str += "00";  
  390.         }  
  391.         else if (!/\.\d{2}$/.test(str) ) {  
  392.             str += ".00";  
  393.         }  
  394.         return str;  
  395.     }  
  396.     else {  
  397.         return "Invalid";  
  398.     }  
  399. }//eof - isCurrency3  

没有评论:

发表评论