日期:2014-05-16  浏览次数:20392 次

使用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 - 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 isS