// 空值驗證
function ValidForm(x, msg) {
    if ($("#" + x).val().length == 0) { alert(msg); $("#" + x).focus(); return false; } else return true;
}
function ValidSelect(x, msg) {
    if ($("#" + x + " option:selected").val() == "" || $("#" + x + " option:selected").val() < 1) {
        alert(msg); $("#" + x).focus(); return false;
    }
    else return true;
}
function ValidEMail(x, msg) {
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

    if ($("#" + x).val().match(emailExp)) return true; else { alert(msg); $("#" + x).focus(); return false; }
}

function ValidURL(x, msg) {
    var UrlExp = /^(https?:\/\/)?((([a-z\d]([a-z\d-]*[a-z\d])*)\.)+[a-z]{2,}|((\d{1,3}\.){3}\d{1,3}))(\:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$/;
    if ($("#" + x).val().match(UrlExp)) return true; else { alert(msg); $("#" + x).focus(); return false; }
}

function isNumeric(x, msg) {
    var numExpression = /^[0-9]+$/;
    if ($("#" + x).val().match(numExpression)) return true; else { alert(msg); $("#" + x).focus(); return false; }
}

function isAlphabet(x, msg) {
    var alphaExp = /^[a-zA-Z]+$/;
    if ($("#" + x).val().match(alphaExp)) return true; else { alert(msg); $("#" + x).focus(); return false; }
}

function isAlphanumeric(x, msg) {
    var alphaExp = /^[0-9a-zA-Z]+$/;
    if ($("#" + x).val().match(alphaExp)) return true; else { alert(msg); $("#" + x).focus(); return false; }
}

function lengthRestriction(x, min, max, what) {
    var uInput = $("#" + x).val();
    if (uInput.length >= min && uInput.length <= max) return true; else { alert("輸入的" + what + "長度有誤"); $("#" + x).focus(); return false; }
}

function madeSelection(elem, helperMsg) {
    if (elem.value == "請選擇") {
        alert(helperMsg);
        elem.focus();
        return false;
    } else {
        return true;
    }
}

function emailValidator(elem, helperMsg) {
    var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    if (elem.value.match(emailExp)) {
        return true;
    } else {
        alert(helperMsg);
        elem.focus();
        return false;
    }
}
