﻿// 0 - 去除前后空格; 1 - 去前导空格; 2 - 去尾部空格
function trim(sInputString, iType) {
    var sTmpStr = ' '
    var i = -1

    if (iType == 0 || iType == 1) {
        while (sTmpStr == ' ') {
            ++i
            sTmpStr = sInputString.substr(i, 1)
        }
        sInputString = sInputString.substring(i)
    }

    if (iType == 0 || iType == 2) {
        sTmpStr = ' '
        i = sInputString.length
        while (sTmpStr == ' ') {
            --i
            sTmpStr = sInputString.substr(i, 1)
        }
        sInputString = sInputString.substring(0, i + 1)
    }
    return sInputString
}

function IsNotEmpty(sender, arguments) {
    var s = arguments.Value;
    if (s.replace(/(^\s*)|(\s*$)/g, '').length > 0) {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}
function IsCode(sender, arguments) {
    var s = arguments.Value;
    var result;
    result = s.match(/^[0-9a-zA-Z]{1,}$/g);
    if (result != null) {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}
function IsEmail(sender, arguments) {
    var s = trim(arguments.Value, 0);
    var result;
    result = s.match(/^[A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,4}$/g);
    if (result != null) {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}
function IsInt(sender, arguments) {
    var s = arguments.Value;
    var result;
    result = s.match(/^[0-9]+$/g);
    if (result != null) {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}
function IsFloat(sender, arguments) {
    var s = arguments.Value;
    var result;
    result = s.match(/^[+|-]?\d*\.?\d*$/);
    if (result != null) {
        arguments.IsValid = true;
    }
    else {
        result = s.match(/^[0-9]+$/g);
        if (result != null) {
            arguments.IsValid = true;
        }
        else {
            arguments.IsValid = false;
        }
    }
}
function IsDate(sender, arguments) {
    var s = arguments.Value;
    var result;
    result = s.match(/^((((((0[48])|([13579][26])|([2468][048]))00)|([0-9][0-9]((0[48])|([13579][26])|([2468][048]))))-02-29)|(((000[1-9])|(00[1-9][0-9])|(0[1-9][0-9][0-9])|([1-9][0-9][0-9][0-9]))-((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30))|(((0[1-9])|(1[0-2]))-((0[1-9])|(1[0-9])|(2[0-8]))))))$/i);
    if (result != null) {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}

function IsDateMonth(sender, arguments) {
    var s = arguments.Value;
    var result;
    result = s.match(/^((((((0[48])|([13579][26])|([2468][048]))00)|([0-9][0-9]((0[48])|([13579][26])|([2468][048]))))-02-29)|(((000[1-9])|(00[1-9][0-9])|(0[1-9][0-9][0-9])|([1-9][0-9][0-9][0-9]))-((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30))|(((0[1-9])|(1[0-2]))-((0[1-9])|(1[0-9])|(2[0-8]))))))$/i);
    if (result != null) {
        arguments.IsValid = true;
    }
    else {
        var result1 = s.match(/^\d{4}-?(?:0[1-9]|1[0-2])$/g);
        if (result1 != null) {
            arguments.IsValid = true;
        }
        else {
            result = s.match(/^\d{4}$/g);
            if (result != null) {
                arguments.IsValid = true;
            }
            else {
                arguments.IsValid = false; 
            }
        }
    }
}
function IsDateTime(sender, arguments) {
    var s = arguments.Value;
    var result;
    result = s.match(/^((((((0[48])|([13579][26])|([2468][048]))00)|([0-9][0-9]((0[48])|([13579][26])|([2468][048]))))-02-29)|(((000[1-9])|(00[1-9][0-9])|(0[1-9][0-9][0-9])|([1-9][0-9][0-9][0-9]))-((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30))|(((0[1-9])|(1[0-2]))-((0[1-9])|(1[0-9])|(2[0-8]))))))$/i);
    if (result != null) {
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}