`

common validation methods

阅读更多

var Messages = {

      ……

      addSuccessMess:"Add Successfully!",

      unnull : " can't be blank.",

      numberrequired : "Please enter a valid positive integer.",

      maxsize : "The maximal size is ",

      maxlength : "the longest character size is ",

      daterequired : "Please enter a valid date in the format 'MM/dd/yyyy'.",

      duplicatedField : " cant not be Duplicated."

 

……

};

 

function checkMaxSize(value, maxsize) {

      if ($.trim(value).length > maxsize) {

            return Messages.maxsize + maxsize + ".";

      }

      return "";

}

 

function checkLength(value, maxlength,titleName) {

      if ($.trim(value).length > maxlength) {

            alert(titleName+","+Messages.maxlength + maxlength + ".");

            return false;

      }

      return true;

}

 

function checkNullAndNumber(param) {

      var val = param.value;

      if (val.length > 0) {

            for ( var i = 0; i < val.length; i++) {

                  c = val.charAt(i).charCodeAt();

                  if (c == 45 || i == 0) {

                        continue;

                  }

                  if (c < 48 || c > 57) {

                        $('.error').show("slow").html("Must be integral number!");

                        event.preventDefault();

                        return false;

                  } else {

                        $('.error').hide();

                  }

            }

      } else {

            $('.error').show("slow").text("Can't be blank.");

            event.preventDefault();

            return false;

      }

}

 

function checkNull(value) {

      if ($.trim(value).length <= 0) {

            return Messages.unnull;

      }

      return "";

}

 

function checkNumber(value) {

      if ($.trim(value).length == 0)

            return "";

 

      for ( var i = 0; i < value.length; i++) {

            c = value.charAt(i).charCodeAt();

            if (c == 45) {

                  continue;

            }

            if (c < 48 || c > 57) {

                  // $('.error').show("slow").html("Must be integral number!");

                  return Messages.numberrequired;

            }

      }

      return "";

}

 

function checkIsPositiveInteger(value){

      var reg = /^\d+$/;

      return reg.test($.trim(value));

}

 

function checkDate(value) {

      if ($.trim(value).length == 0)

            return "";

 

      var r = value.match(/(0[1-9]|1[012])[\/](0[1-9]|[12][0-9]|3[01])[\/](19|20)\d\d/);

      if (r == null) {

            return Messages.daterequired;

      }

      return "";

}

 

function checkStartDateGreatThanEndDate(startDate,endDate){

      if(startDate.length>0&&endDate.length>0){

            if( Date.parse(startDate)>Date.parse(endDate)){

             return false;

            }

         }

       return true;

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics