Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 6.17 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //UI for validation
  2.  
  3. function validationMessages() {
  4.  
  5.   //*** 'me' acts as an alias that can be used within the methods
  6.   var me = this;
  7.   var imgPath=Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,"images/bubble.png");
  8.  
  9.   validationMessages.totalErrors = 0;
  10.   validationMessages.reqdfieldsRemaining = 0;
  11.  
  12.   this.messageWin = Titanium.UI.createWindow({
  13.     height: 50,
  14.     width:310,
  15.     top:40,
  16.     touchEnabled:false,
  17.     visible:true,
  18.     zIndex: 999,
  19.     orientationModes : [
  20.     Titanium.UI.PORTRAIT
  21.     ]
  22.   });
  23.  
  24.   this.valView =  Titanium.UI.createView({
  25.     width:310,
  26.     height:50,
  27.     //backgroundColor:'Red',
  28.     //borderRadius:10,
  29.     opacity:1.0,
  30.     touchEnabled:false,
  31.     visible:false,
  32.     backgroundImage: imgPath.nativePath
  33.     //left:35
  34.   });
  35.  
  36.   this.valLabel =  Titanium.UI.createLabel({
  37.     text:'Missing or error in field (see red text)',
  38.     color:'#fff',
  39.     width:300,
  40.     height:50,
  41.     top:2,
  42.     font: {
  43.       fontFamily:'Helvetica Neue',
  44.       fontSize:16,
  45.       fontWeight:'bold'
  46.     },
  47.     textAlign:'center'
  48.   });
  49.  
  50.   this.valView.add(this.valLabel);
  51.  
  52.   this.messageWin.add(this.valView);
  53.  
  54.   this.anim_out = Titanium.UI.createAnimation();
  55.   this.anim_out.opacity=0;
  56.   this.anim_out.duration = 4000;
  57.  
  58.   //function to display view
  59.   validationMessages.prototype.displayValErr  = function displayValErr() {
  60.     this.valView.opacity = 1.0;
  61.     this.valView.visible = true;
  62.     this.valView.show();
  63.     this.messageWin.open();
  64.     this.valView.animate(this.anim_out);
  65.     // setTimeout(function()
  66.     // {
  67.     // //for fading out error tip and closing
  68.     // this.valView.hide();
  69.     // //messageWin.close({opacity:0,duration:500});
  70.     // },4000);
  71.     //}
  72.   };
  73.   //test is Valid Number => returns bool
  74.   this.isValidNumber = function isValidNumber(val) {
  75.     var re= /^[\-+]?\d+$/;
  76.     Titanium.API.debug('isValidNumber:' + re.test(val));
  77.     return re.test(val);
  78.   };
  79.  
  80.   //test for integer
  81.   this.isInteger = function isInteger(val) {
  82.     var re= /^[\-+]?\d+$/;
  83.     Titanium.API.debug('isInteger:' + re.test(val));
  84.     return re.test(val);
  85.   };
  86.  
  87.   //function to test double **** maynot be correct ***
  88.   this.isDouble = function isDouble(val) {
  89.     var re = new RegExp("[0-9.]");
  90.     Titanium.API.debug('isDouble:' + re.test(val));
  91.     return re.test(val);
  92.   };
  93.  
  94.   this.isNumber = function isNumber(val) { // REAL NUMBERS
  95.     var re = /^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/;
  96.     Titanium.API.debug('isNumber:' + re.test(val));
  97.     return re.test(val);
  98.   };
  99.  
  100.   //test for real val
  101.   this.isReal = function isReal(val) {
  102.     var re =/^[\-+]?\d*\.?\d+$/;
  103.     Titanium.API.debug('isReal:' + re.test(val));
  104.     return re.test(val);
  105.   };
  106.  
  107.   this.removeLastEntry = function removeLastEntry(val) {
  108.     Titanium.API.debug('removeLastEntry:');
  109.     return  val.slice(0,val.toString().length-1);
  110.   };
  111.  
  112.   //test for empty
  113.   // check to see if input is whitespace only or empty
  114.   this.isEmpty = function isEmpty( val ) {
  115.     if ( null === val || "" === val ) {
  116.       return true;
  117.     } else {
  118.       return false;
  119.     }
  120.   };
  121.  
  122.   //test if reqd
  123.   this.isPresent = function isPresent(val) {
  124.     var re = /[^.*]/;
  125.     Titanium.API.debug('isPresent:' + re.test(val));
  126.     return re.test(val);
  127.   };
  128.  
  129.   //check for value in range
  130.   // check to see if value is within min and max
  131.   this.isWithinRange = function isWithinRange(val, min, max) {
  132.     if (val >= min && val <= max) {
  133.       Titanium.API.debug('isWithinRange:true');
  134.       return true;
  135.     } else {
  136.       Titanium.API.debug('isWithinRange:false');
  137.       return false;
  138.     }
  139.   };
  140.  
  141.   // check to see if value is within min chars
  142.   this.isMinChars = function isMinChars(val, min) {
  143.     if (val.toString().length >= min) {
  144.       Titanium.API.debug('isMinChars:true');
  145.       return true;
  146.     } else {
  147.       Titanium.API.debug('isMinChars:false');
  148.       return false;
  149.     }
  150.   };
  151.  
  152.   // check to see if value is within max chars
  153.   this.isWithinMaxChars = function isWithinMaxChars(val, max) {
  154.     if (val.toString().length <= max) {
  155.       Titanium.API.debug('isWithinMaxChars:true');
  156.       return true;
  157.     } else {
  158.       Titanium.API.debug('isWithinMaxChars:false');
  159.       return false;
  160.     }
  161.   };
  162.  
  163.   this.checkValidation = function checkValidation(obj) {
  164.     var isValid=null;
  165.     Titanium.API.info('checking validation');
  166.     //clear validation
  167.     obj.color = obj.validation.color;
  168.  
  169.     //keep record of validation colors
  170.     if(!obj.validation.color) {
  171.       obj.validation.color = obj.color;
  172.       obj.validation.backgroundColor = obj.backgroundColor;
  173.     }
  174.     //set valuation highlight effect
  175.     function setEffect(obj,isOff) {
  176.       if (isValid===false) {
  177.         return false;
  178.       } else {
  179.         if(!isOff) {
  180.           obj.color = 'Red';
  181.           obj.backgroundColor = 'Red';
  182.           isValid = false;
  183.         }
  184.         if(isOff) {
  185.           obj.color = obj.validation.color;
  186.           obj.backgroundColor = obj.validation.backgroundColor;
  187.           isValid = true;
  188.         }
  189.       }
  190.       return isOff;
  191.     }
  192.  
  193.     //check if reqd
  194.     if(obj.validation.reqd) {
  195.       setEffect(obj, this.isPresent(obj.value));
  196.     }
  197.  
  198.     ///validation checks only if Value Present
  199.     if(this.isPresent(obj.value)) {
  200.       //check for double value
  201.       if(obj.validation.isdouble) {
  202.         setEffect(obj,this.isDouble(obj.value));
  203.       }
  204.       //check if need integer
  205.       if(obj.validation.isinteger) {
  206.         setEffect(obj,this.isInteger(obj.value));
  207.         // if (!setEffect(obj,isInteger(obj.value)))
  208.         // obj.value = removeLastEntry(obj.value);
  209.       }
  210.       //check if need min
  211.       if(obj.validation.minchars) {
  212.         setEffect(obj,this.isMinChars(obj.value,obj.validation.minchars));
  213.       }
  214.       //check if max
  215.       if(obj.validation.maxchars) {
  216.         setEffect(obj,this.isWithinMaxChars(obj.value,obj.validation.maxchars));
  217.         // removed next cos the check still returns false ....
  218.         // if(!isWithinMaxChars(obj.value,obj.validation.maxchars))
  219.         // {
  220.         // obj.value = removeLastEntry(obj.value);
  221.         // checkValidation(obj);
  222.         // }
  223.       }
  224.       //check within range
  225.       if(obj.validation.range) {
  226.         setEffect(obj,this.isWithinRange(obj.value,obj.validation.range.min,obj.validation.range.max));
  227.       }
  228.     }
  229.  
  230.     Ti.API.info('isValid:' + isValid);
  231.     return isValid;
  232.  
  233.   };
  234. }