ass3js

function register(){
    var uname = document.getElementById("username").value;
    var email = document.getElementById("email").value;
    var password = document.getElementById("password").value;
    var confirm = document.getElementById("confirm").value;
    var phone = document.getElementById("telephone").value;
    var website = document.getElementById("website").value;
    
    var error = false;
    
    if(checkEmpty(uname)) {
        resetErrorMsg("unameError","user name cannot be empty!");
        error = true;
    }else {
        resetErrorMsg("unameError","");
    }
    if(checkEmpty(email)){
        resetErrorMsg("emailError","email address cannot be empty!");
        error = true;
    }
    else if(!checkEmail(email)) {
        resetErrorMsg("emailError","not a valid email address!");
        error = true;
    }else {
        resetErrorMsg("emailError","");
    }
    if(checkEmpty(password)){
        resetErrorMsg("passwdError","password cannot be empty");
        error =true;
    }
    else if(!checkPw(password)){
        resetErrorMsg("passwdError","password should be 6~16 characters!");
        error =true;
    }else {
        resetErrorMsg("passwdError","");
    }
    if(checkEmpty(confirm)) {
        resetErrorMsg("confirmError","comfirm password cannot be empty!");
        error = true;
    }
    else if(!checkConfirm(password,confirm)) {
        resetErrorMsg("confirmError","not the same password!");
        error = true;
    }else {
        resetErrorMsg("confirmError","");
    }    
    if(!checkPhone(phone)){
        resetErrorMsg("phoneError","not a valid phone number!");
        error = true;
    }else {
        resetErrorMsg("phoneError","");
    }
    
    if(!checkWeb(website)){
        resetErrorMsg("webError","not a valid URL!");
        error = true;
    }else {
        resetErrorMsg("webError","");
    }
return !error;
}

function resetForm(){
    document.forms[0].reset();
    resetErrorMsg("unameError","");
    resetErrorMsg("emailError","");
    resetErrorMsg("passwdError","");
    resetErrorMsg("confirmError","");
    resetErrorMsg("phoneError","");
    resetErrorMsg("webError","");
}

function resetErrorMsg(msgId, msg){
    document.getElementById(msgId).innerHTML = msg;
}

function checkEmpty(strValue){
    return (strValue==null || strValue=="");
}

function checkEmail(email){
    var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

function checkPw(passwd){
    return (passwd.length>=6 && passwd.length<=16);
}

function checkConfirm(passwd,passwd2){
    return passwd==passwd2;
}

function checkPhone(phoneNo) {
    if(checkEmpty(phoneNo)) {
        return true;
    }
    var re = /^(\()?\d{3}(\))?(-|\s)?\d{3}(-|\s)\d{4}$/;
    return re.test(phoneNo);
}

function checkWeb(website) {
    if(checkEmpty(website)) {
        return true;
    }
    //var re = new RegExp("(http|ftp|https)://[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:/~+#-]*[\w@?^=%&amp;/~+#-])?");
    //var re = /(http|ftp|https):\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&amp;:\/~+#-]*[\w@?^=%&amp;\/~+#-])?/;
    //from http://someweblog.com/url-regular-expression-javascript-link-shortener/
    var re = /\(?(?:(http|https|ftp):\/\/)?(?:((?:[^\W\s]|\.|-|[:]{1})+)@{1})?((?:www.)?(?:[^\W\s]|\.|-)+[\.][^\W\s]{2,4}|localhost(?=\/)|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?::(\d*))?([\/]?[^\s\?]*[\/]{1})*(?:\/?([^\s\n\?\[\]\{\}\#]*(?:(?=\.)){1}|[^\s\n\?\[\]\{\}\.\#]*)?([\.]{1}[^\s\?\#]*)?)?(?:\?{1}([^\s\n\#\[\]]*))?([\#][^\s\n]*)?\)?/gi;
    return re.test(website);
}

posted on 2016-07-24 22:45  那年的村子  阅读(522)  评论(0编辑  收藏  举报