Ajax 비동기 방식 으로 로그 인 및 검증 실현

이 사례 는 Ajax 비동기 방식 으로 로그 인 과 검증 을 실현 하 는 상세 한 코드 를 설명 하여 여러분 께 참고 하도록 공유 합 니 다.구체 적 으로 다음 과 같다.
로그 인 코드
이것 은 Bootstrap 3 의 구성 요소 기능 을 사용 하여 이 루어 진 것 입 니 다.

<div class="login_con_R">
      <h4>  </h4>
      <FORM id="loginFormId" class="form-horizontal" action="login" method="post">
        <div class="form-group input-group">
          <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
          <input type="text" class="form-control" placeholder="   " name="username" id="j_username" value="koala">
        </div>
        <div class="form-group input-group">
          <span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>
          <input type="password" name="password" id="j_password" class="form-control" placeholder="  " value="888888"/>
        </div>       
        <div class="form-group input-group" style="margin-top: 45px;">
          <button type="button" class="btn btn-primary btn-block" id="loginBtn">  </button>
        </div>
      </FORM>
    </div>
비동기 로그 인 요청 코드

<script>
  $(function(){
    var btnLogin = $('#loginBtn');
    var form = $('#loginFormId');
    $('body').keydown(function(e) {
      if (e.keyCode == 13) {
        dologin();
      }
    });
    btnLogin.on('click',function() {
      dologin();
    });

    var dologin = function() {
      var userNameElement = $("#j_username");
      var passwordElement = $("#j_password");
      var username = userNameElement.val();
      var password = passwordElement.val();
      if (!Validation.notNull($('body'), userNameElement, username, '       ')) {
        return false;
      }
      if (!Validation.notNull($('body'), passwordElement, password, '      ')) {
        return false;
      }
      btnLogin.attr('disabled', 'disabled').html('    ...');
      var param = form.serialize();
      $.ajax({
        url: contextPath+"/login.koala",
        dataType: "json",
        data: param,
        type: "POST",
        success: function(data){
          if(data.success){
            $('.login_con_R').message({
              type: 'success',
              content: '    !'
            });
            /*         ,          ,     index  */
            window.location.href=contextPath+"/index.koala";
          }else{
            btnLogin.removeAttr('disabled').html('  ');
            $('.login_con_R').message({
              type: 'error',
              content: data.errorMessage
            });
          }
        }
      });
    };
    }); 

  </script>

사용 한 검증 js 코드 Validation.js

Validator = {
  Require : {expression:/.+/,errorMsg:"       "},
  Email : {expression:/^\w+([-+.]\w+)*@\w+([-.]\\w+)*\.\w+([-.]\w+)*$/,errorMsg:"Email     "},
  Mobile:{expression:/^(1[3|5|8]{1}\d{9})$/,errorMsg:"       "},
  TelePhone:{expression:/^(((0\d{2,3}-)?\d{7,8}(-\d{4})?))$/,errorMsg:"         "},
  IdCard:{expression:/^\d{15}(\d{2}[A-Za-z0-9])?$/,errorMsg:"          "},
  Number:{expression:/^-?(\d+|[1-9]\d*\.\d+|0\.\d*[1-9]\d*|0?\.0+|0)$/,errorMsg:"     "},
  English:{expression:/^[A-Za-z]+$/,errorMsg:"       "},
  Chinese:{expression:/^[\u0391-\uFFE5]+$/,errorMsg:"       "},
  URL:{expression:/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/,errorMsg:"URL       "},
  Regex:{errorMsg:"     "},
  UnSafe : /^(([A-Z]*|[a-z]*|\d*|[-_\~!@#\$%\^&\*\.\(\)\[\]\{\}<>\?\\\/\'\"]*)|.{0,5})$|\s/,
  IsSafe : function(str){return !this.UnSafe.test(str);},
  SafeString : {expression:"this.IsSafe(value)",errorMsg:"       "},
  ErrorItem : [document.forms[0]],
  ErrorMessage : ["          :\t\t\t\t"],
  Validate : function(theForm, mode){
    var obj = theForm || event.srcElement;
    var count = obj.elements.length;
    this.ErrorMessage.length = 1;
    this.ErrorItem.length = 1;
    this.ErrorItem[0] = obj;
    for(var i=0;i<count;i++){
      with(obj.elements[i]){
        if(disabled)continue;
        var _dataType = getAttribute("dataType");
        if(typeof(_dataType) == "object" || typeof(this[_dataType]) == "undefined") continue;
        this.ClearState(obj.elements[i]);
        if(getAttribute("require") == "false" && value == "") continue;
        var errorMsg = getAttribute("errorMsg")? getAttribute("errorMsg") : this[_dataType].errorMsg;
        errorMsg = errorMsg || "invalid";
        var validateExpr = getAttribute("validateExpr");
        validateExpr = validateExpr || this[_dataType].expression;
        switch(_dataType){
          case "Date" :
          case "Repeat" :
          case "Range" :
          case "Compare" :
          case "Custom" :
          case "Group" :
          case "Limit" :
          case "LimitB" :
          case "SafeString" :
            if(!eval(validateExpr)) {
              this.AddError(i, errorMsg);
            }
            break;
          default :
            if(!eval(validateExpr).test(value)){
              this.AddError(i, errorMsg);
            }
            break;
        }
      }
    }
    if(this.ErrorMessage.length > 1){
      var content = this.ErrorMessage[1].replace(/\d+:/," ");
      var $element = $(this.ErrorItem[1]);
      var name = $element.attr('name');
      if(name.indexOf('DTO') != -1){
        name = name.split('.')[1];
      }
      var $targetElement = $element;
      var $tempElement = $targetElement.closest('form').find('#' + name + 'ID');
      if($tempElement.hasClass('select')){
        $targetElement = $tempElement;
      }
      showErrorMessage($element.closest('.modal'), $targetElement, content);
      return false;
    }
    return true;
  },
  limit : function(len,min, max){
    min = min || 0;
    max = max || Number.MAX_VALUE;
    return min <= len && len <= max;
  },
  LenB : function(str){
    return str.replace(/[^\x00-\xff]/g,"**").length;
  },
  ClearState : function(elem){
    with(elem){
      if(style.color == "red")
        style.color = "";
      var ligertipid = getAttribute("ligertipid");
      if(ligertipid && ligertipid != ""){
        removeAttribute("ligertipid");
        $(elem).ligerHideTip();
      }
    }
  },
  AddError : function(index, str){
    this.ErrorItem[this.ErrorItem.length] = this.ErrorItem[0].elements[index];
    this.ErrorMessage[this.ErrorMessage.length] = this.ErrorMessage.length + ":" + str;
  },
  Exec : function(op, reg){
    return new RegExp(reg,"g").test(op);
  },
  compare : function(op1,operator,op2){
    switch (operator) {
      case "NotEqual":
        return (op1 != op2);
      case "GreaterThan":
        return (op1 > op2);
      case "GreaterThanEqual":
        return (op1 >= op2);
      case "LessThan":
        return (op1 < op2);
      case "LessThanEqual":
        return (op1 <= op2);
      default:
        return (op1 == op2);
    }
  },
  MustChecked : function(name, min, max){
    var groups = document.getElementsByName(name);
    var hasChecked = 0;
    min = min || 1;
    max = max || groups.length;
    for(var i=groups.length-1;i>=0;i--)
      if(groups[i].checked) hasChecked++;
    return min <= hasChecked && hasChecked <= max;
  },
  IsDate : function(op, formatString){
    formatString = formatString || "ymd";
    var m, year, month, day;
    switch(formatString){
      case "ymd" :
        m = op.match(new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$"));
        if(m == null ) return false;
        day = m[6];
        month = m[5]--;
        year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10));
        break;
      case "dmy" :
        m = op.match(new RegExp("^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$"));
        if(m == null ) return false;
        day = m[1];
        month = m[3]--;
        year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10));
        break;
      default :
        break;
    }
    var date = new Date(year, month, day);
    return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate());
    function GetFullYear(y){return ((y<30 ? "20" : "19") + y)|0;}
  }
};

var Validation = {
  notNull: function($container, $element, content, errorMessage) {
    if (isNull(content)) {
      showErrorMessage($container, $element, errorMessage);
      return false;
    }
    return true;
  },
  email: function($container, $element, content, errorMessage) {
    var reg = new RegExp(/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/);
    if (!reg.test(content)) {
      showErrorMessage($container, $element, errorMessage);
      return false;
    }
    return true;
  },
  serialNmuber: function($container, $element, content, errorMessage) {
    var reg = new RegExp(/^[0-9a-zA-Z_-]*$/);
    if (!reg.test(content)) {
      showErrorMessage($container, $element, errorMessage);
      return false;
    }
    return true;
  },
  number: function($container, $element, content, errorMessage) {
    var reg = new RegExp(/^[0-9]*$/);
    if (!reg.test(content)) {
      showErrorMessage($container, $element, errorMessage);
      return false;
    }
    return true;
  },
  humanName: function($container, $element, content, errorMessage) {
    var reg = new RegExp(/(^[A-Z a-z]*$)|(^[\u4e00-\u9fa5]*$)/);
    if (!reg.test(content)) {
      showErrorMessage($container, $element, errorMessage);
      return false;
    }
    return true;
  },
  checkByRegExp: function($container, $element, regExp, content, errorMessage){
    var reg = new RegExp(regExp);
    if (!reg.test(content)) {
      showErrorMessage($container, $element, errorMessage);
      return false;
    }
    return true;
  }
};

/**
 *       
 */
var isNull = function(item){
  if(item == null || item == "" || item.replace(/(^\s*)|(\s*$)/g, "") == "" ){
    return true;
  }
  return false;
};

/**
 *       
 */
var showErrorMessage = function($container, $element, content){
  $element.popover({
    content: content,
    trigger: 'manual',
    container: $container
  }).popover('show').on({
      'blur': function(){
        $element.popover('destroy');
        $element.parent().removeClass('has-error');
      },
      'keydown': function(){
        $element.popover('destroy');
        $element.parent().removeClass('has-error');
      }
    }).focus().parent().addClass('has-error');
};
본문 에서 말 한 것 이 모두의 학습 에 도움 이 되 기 를 바란다.

좋은 웹페이지 즐겨찾기