Venciity

Kaskata validations

Nov 14th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ---------- string is null or empty ----------
  2. String.prototype.isNullOrEmpty = function (varName) {
  3.     if (typeof (this.valueOf()) != 'string') {
  4.         throw new Error(varName + "must be String");
  5.     }
  6.    
  7.     if (!this.valueOf()) {
  8.         throw new Error(varName + " cannot be undefined or empty");
  9.     }
  10. }
  11.  
  12. // ---------- is integer in range ----------
  13. Number.prototype.isIntegerAndInRange = function (minValue, maxValue, varName) {
  14.     if (typeof ( this.valueOf() ) != 'number') {
  15.         throw new Error(varName + " must be Number.");
  16.     }
  17.     if (this === parseInt( this.valueOf() , 10)) {
  18.         throw new Error(varName + " must be Integer.");
  19.     }
  20.     if (this.valueOf() < minValue || this.valueOf() > maxValue) {
  21.         throw new Error(varName + " must be Integer in range[" + minValue + "..." + maxValue + "].");
  22.     }
  23.     return true;
  24. }
  25.  
  26. // ---------- is boolean ----------
  27. Boolean.prototype.isBoolean = function (varName) {
  28.     if (typeof (this.valueOf()) != 'boolean') {
  29.         throw new Error(varName + " must be Boolean value.");
  30.     }
  31.     return true;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment