/**
 * Created by Administrator on 2016/7/26.
 */
(function () {
    var $errorName = $('#errorName');
    var $errorTel = $('#errorTel');
    var $userName = $('#userName');
    var $userTel = $('#userTel');
    var $submit = $('#submit');
    var $form=$('#form');

    var myBalance = $('#myBalance').html();
    var total = $('#total').html();
    var progressBar = parseFloat($('#progressBar').css('width'));

    $('#progressBar i').animate({
        width:myBalance/total*progressBar+'px',
        opacity:1
    },1000);
    /**
     * 验证用户姓名
     * @param obj
     * @returns {boolean}
     */
    function valiUserName(userName) {
        $errorName.html(userName ? '' : '用户名不能为空，请重新输入！');
        return !userName;
    }

    /**
     * 验证电话号码
     * @param obj
     * @returns {boolean}
     */
    function valiMobile(mobile) {
        var msg = /^[\d]/g.test(mobile) ? '' : '手机号有误，请重新输入！';
        /^[\d]/g.test(mobile[mobile.length-1])? $userTel.val(mobile) : $userTel.val(mobile.slice(0,mobile.length-1));
        console.log(mobile[mobile.length-1]);
        console.log(/^[\d]/g.test(mobile[mobile.length-1]));
        $errorTel.html(msg);
        return !mobile;
    }

    $userName.on('keyup', function () {
        valiUserName($.trim($userName.val()+''));
    });
    $userTel.on('keyup', function () {
        valiMobile($userTel.val());
    });

    /**
     * 提交验证
     */
    $form.on('submit',function (e) {
        if( valiUserName($.trim($userName.val()+'')) ){
            $userName.focus();
            e.preventDefault();
            $errorName.html('用户名输入有误,请再次输入!');
            return;
        }
        if( valiMobile( $.trim( $userTel.val()+'' ) ) ){
            $userTel.focus();
            e.preventDefault();
            $errorTel.html('密码有误,请再次输入!');
            return;
        }

    });


})();
