应用JavaScript正则表达式验证

使用JavaScript正则表达式验证/** regexp-validate-data.js **/function anyChar(str) {/* Verify at leas

使用JavaScript正则表达式验证

/** regexp-validate-data.js **/function anyChar(str) {    /* Verify at least one nonspace character     *    or string of characters     * Return boolean     */    return /\S+/.test(str);}//eof - anyCharfunction 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 - anyWordfunction any3letters(str) {    /* At least 3 consecutive letters anywhere in str     *    case insensitive     * Return boolean     */    return /[a-z]{3}/i.test(str);}//eof - any3lettersfunction 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 - any10Charfunction 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 - anyVar10function 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 - anyClass5function 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 - any4Wordsfunction 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 - three3letteredfunction 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 - varTestfunction 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 - classTestfunction 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 - phpTestfunction 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 - isEmail1function isEmail2(data) {    return /^[\w.\-]+@[\w\-]+\.[a-zA-Z0-9]+$/i.test(data);}//eof - isEmail2function isEmail3(data) {    //recommended expression: most complete    return /^([\w]+)(\.[\w]+)*@([\w\-]+)(\.[\w]{2,4})(\.[a-z]{2})?$/i.test(data);}//eof - isEmail3function isSSN1(data) {    /* Validate American SSN requiring dashes     * Return boolean     */    return /^\d{3}-\d{2}-\d{4}$/.test(data);}//eof - isSSN1function isSSN2(data) {    /* Validate American SSN dashes optional     * Return boolean     */    return /^\d{3}-?\d{2}-?\d{4}$/.test(data);}//eof -isSSN2function 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 - isPhone1function 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 - isPhone2function 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 - isPhone3function isValidDate(date_string, format) {/* Validate string user entered as a date in  * 1 of 6 formats *m/d/yAmerican month, day, year with 2 or 4 digit year *mm/dd/yyShort American with 2 digit year *mm/dd/yyyyLong American with 4 digit year *y/m/dEuropean year, month, day with 2 or 4 digit year *yy/mm/ddEuropean with 2 digit year *yyyy/mm/ddEuropean 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 formatif (test) {date_parts = date_string.match(test.re); //parse stringif (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 monthstest = (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 - uIntegerfunction uInteger2(str) {    /* Verify unsigned integer     *        ignoring leading and trailing spaces     * Return boolean     */    str = str.replace(/^\s+|\s+$/g, '');    return /^\d+$/.test(str);}//eof uInteger2function 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 - uInteger3to5function 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 - sIntegerfunction 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 - sInteger2function 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 - sInteger3to5function 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 isFloatfunction 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 isFloat2function 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 - isFloat3to5d10to3function 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 - isCurrency1function 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 - isCurrency2function 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