Skip to content

Instantly share code, notes, and snippets.

@imCoding
Forked from coomsie/validation.js
Created February 9, 2014 14:11
Show Gist options
  • Save imCoding/8899627 to your computer and use it in GitHub Desktop.
Save imCoding/8899627 to your computer and use it in GitHub Desktop.

Revisions

  1. @coomsie coomsie revised this gist Aug 10, 2011. 1 changed file with 187 additions and 79 deletions.
    266 changes: 187 additions & 79 deletions validation.js
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,21 @@
    //UI for valiation

    function validationMessages(){
    function validationMessages() {

    //*** 'me' acts as an alias that can be used within the methods
    var me = this;
    var imgPath=Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,"images/bubble.png");

    validationMessages.totalErrors =0;
    validationMessages.reqdfieldsRemaining=0;

    this.messageWin = Titanium.UI.createWindow({
    height: 50,
    width:310,
    top:40,
    touchEnabled:false,
    visible:true,
    zIndex: 999,
    orientationModes : [
    Titanium.UI.PORTRAIT
    ]
    zIndex: 999
    });

    this.valView = Titanium.UI.createView({
    @@ -32,7 +29,7 @@ function validationMessages(){
    backgroundImage: imgPath.nativePath
    //left:35
    });

    this.valLabel = Titanium.UI.createLabel({
    text:'Missing or error in field (see red text)',
    color:'#fff',
    @@ -48,27 +45,32 @@ function validationMessages(){
    });

    this.valView.add(this.valLabel);

    this.messageWin.add(this.valView);

    this.anim_out = Titanium.UI.createAnimation();
    this.anim_out.opacity=0;
    this.anim_out.duration = 4000;

    //function to display view
    validationMessages.prototype.displayValErr = function displayValErr()
    {
    validationMessages.prototype.displayValErr = function displayValErr(errMsg) {
    if(errMsg !=='' && errMsg !== undefined && errMsg !== null){
    this.valLabel.text = errMsg;
    }else{ //default
    this.valLabel.text = 'Missing or error in field (see red text)';
    }

    this.valView.opacity = 1.0;
    this.valView.visible = true;
    this.valView.visible = true;
    this.valView.show();
    this.messageWin.open();
    this.valView.animate(this.anim_out);
    // setTimeout(function()
    // {
    // //for fading out error tip and closing
    // this.valView.hide();
    // //messageWin.close({opacity:0,duration:500});
    // },4000);
    // {
    // //for fading out error tip and closing
    // this.valView.hide();
    // //messageWin.close({opacity:0,duration:500});
    // },4000);
    }
    }

    @@ -87,7 +89,7 @@ function isInteger(val) {
    }

    //function to test double **** maynot be correct ***
    function isDouble(val){
    function isDouble(val) {
    var re = new RegExp("[0-9.]");
    Titanium.API.debug('isDouble:' + re.test(val));
    return re.test(val);
    @@ -96,7 +98,7 @@ function isDouble(val){
    function isNumber(val) { // REAL NUMBERS
    var re = new RegExp("/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/");
    Titanium.API.debug('isNumber:' + re.test(val));
    return re.test(val);
    return re.test(val);
    }

    //test for real val
    @@ -162,73 +164,179 @@ function isWithinMaxChars(val, max) {
    }
    }

    function checkValidation(obj) {
    var rules = {};

    function setValidationRules(val) {
    rules = val;
    }

    //get valid rule from rules of field obj
    function getRule(id) {
    var r;
    for (var i = rules.length - 1; i >= 0; i--) {
    ///Ti.API.debug('rule' + i + rules[i]);
    if (rules[i].hasOwnProperty(id))
    r = rules[i][id]; //use bracket notatio when dynamic
    };
    return r;
    }

    function checkValidation(obj , fieldid) {
    var isValid=null;
    Titanium.API.info('checking validation');
    //clear validation
    obj.color = obj.validation.color;

    //keep record of validation colors
    if(!obj.validation.color){
    obj.validation.color = obj.color;
    obj.validation.backgroundColor = obj.backgroundColor;
    }
    //set valuation highlight effect
    function setEffect(obj,isOff) {
    if (isValid===false) {
    return false;
    } else {
    if(!isOff) {
    obj.color = 'Red';
    obj.backgroundColor = 'Red';
    isValid = false;
    }
    if(isOff) {
    obj.color = obj.validation.color;
    obj.backgroundColor = obj.validation.backgroundColor;
    isValid = true;
    }
    Ti.API.debug('fieldid'+fieldid);

    if(fieldid === undefined)
    fieldid = obj.id;

    var objRule = getRule(fieldid) //get rule for object

    //if no rule then do nothing
    if (objRule !== undefined) {
    Ti.API.debug('the rules' + objRule);
    for(var key in objRule) {
    Ti.API.debug(key);
    }
    return isOff;
    };

    //check if reqd
    if(obj.validation.reqd) {
    setEffect(obj,isPresent(obj.value));
    };
    Titanium.API.info('checking validation');
    //clear validation
    obj.color = obj.validation.color;

    ///validation checks only if Value Present
    if(isPresent(obj.value)) {
    //check for double value
    if(obj.validation.isdouble) {
    setEffect(obj,isDouble(obj.value));
    };
    //check if need integer
    if(obj.validation.isinteger) {
    setEffect(obj,isInteger(obj.value));
    // if (!setEffect(obj,isInteger(obj.value)))
    // obj.value = removeLastEntry(obj.value);
    };
    //check if need min
    if(obj.validation.minchars) {
    setEffect(obj,isMinChars(obj.value,obj.validation.minchars));
    };
    //check if max
    if(obj.validation.maxchars) {
    setEffect(obj,isWithinMaxChars(obj.value,obj.validation.maxchars));
    // removed next cos the check still returns false ....
    // if(!isWithinMaxChars(obj.value,obj.validation.maxchars))
    // {
    // obj.value = removeLastEntry(obj.value);
    // checkValidation(obj);
    // }
    //keep record of validation colors
    if(!obj.validation.color) {
    obj.validation.color = obj.color;
    obj.validation.backgroundColor = obj.backgroundColor;
    }
    //set valuation highlight effect
    function setEffect(obj,isOff, errMsg) {
    if (isValid===false) {
    return false;
    } else {
    if(!isOff) {
    if (obj.color !== 'undefined') {
    obj.color = 'Red';
    obj.backgroundColor = 'Red';
    }
    isValid = false;
    if (obj.errMsg ===''){
    obj.errMsg = errMsg;
    }
    }
    if(isOff) {
    if (obj.color !== 'undefined') {
    obj.color = obj.validation.color;
    obj.backgroundColor = obj.validation.backgroundColor;
    }
    isValid = true;
    if (obj.errMsg !== undefined){
    obj.errMsg = '';
    }
    }
    }
    return isOff;
    };
    //check within range
    if(obj.validation.range) {
    setEffect(obj,isWithinRange(obj.value,obj.validation.range.min,obj.validation.range.max));

    //run engine check if field of the whole form
    validationEngine(obj, objRule);

    function validationEngine(obj , objRule) {
    //check if reqd
    if(objRule.reqd) {
    setEffect(obj,isPresent(obj.value),'Value is required');
    };

    ///validation checks only if Value Present
    if(isPresent(obj.value)) {
    //check for double value
    if(objRule.isdouble) {
    setEffect(obj,isDouble(obj.value),'Value must be an number');
    };
    //check if need integer
    if(objRule.isinteger) {
    setEffect(obj,isInteger(obj.value),'Value must be an whole number');
    // if (!setEffect(obj,isInteger(obj.value)))
    // obj.value = removeLastEntry(obj.value);
    };
    //check if need min
    if(objRule.minchars) {
    setEffect(obj,isMinChars(obj.value,objRule.minchars),'Value under min character(s)');
    };
    //check if max
    if(objRule.maxchars) {
    setEffect(obj,isWithinMaxChars(obj.value,objRule.maxchars),'Value over the max character(s)');
    // removed next cos the check still returns false ....
    // if(!isWithinMaxChars(obj.value,obj.validation.maxchars))
    // {
    // obj.value = removeLastEntry(obj.value);
    // checkValidation(obj);
    // }
    };
    //check within range
    if(objRule.range) {
    setEffect(obj,isWithinRange(obj.value,objRule.range.min,objRule.range.max),'Value is not within the range');
    };
    };
    };
    };

    }; //if rule

    Ti.API.info('isValid:' + isValid);
    return isValid;
    };
    };

    //
    // //test valid email address => returns bool
    // function isValidEmail(emailAddress) {
    // var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    // return re.test(emailAddress);
    // }
    //

    //
    // //test is alpha
    // function isAlpha(val) {
    // var re = /^[a-z ._-]+$/i
    // return re.test(val);
    // }
    //
    // //test alphas numercial
    // function isAlphaNum(val) {
    // var re = /^[a-z0-9 ._-]+$/i
    // return re.test(val);
    // }

    // //test for phone
    // function isPhone(val) {
    // var re = /^[\d\s ().-]+$/
    // return re.test(val);
    // }
    //

    // //test for url
    // function isUrl(val) {
    // var re = /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i
    // return re.test(val);
    // }

    //validation model
    var validator = {
    messages: {
    isinteger: {
    eval: "Value must be an integer"
    },
    isdouble: {
    eval: "Value must be an integer"
    },
    minchars: {
    eval: "Value doesnt meet the min character(s) required"
    },
    maxchars: {
    eval: "Value doesnt meet the max character(s) required"
    },
    maxrange: {
    eval: "Value is more the max allowed"
    },
    minrange: {
    eval: "Value is less the min allowed"
    }
    }
    }; ///end of function
  2. @coomsie coomsie revised this gist Jun 7, 2011. 1 changed file with 102 additions and 181 deletions.
    283 changes: 102 additions & 181 deletions validation.js
    Original file line number Diff line number Diff line change
    @@ -1,39 +1,76 @@
    //UI for valiation
    var imgPath=Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,"images/bubble.png");

    var valView = Titanium.UI.createView({
    width:205,
    height:30,
    backgroundColor:'Red',
    borderRadius:10,
    opacity:0.7,
    touchEnabled:false,
    visible:false,
    //backgroundImage: "images/bubble.png",
    top:2,
    left:50
    });

    Ti.API.info(valView.backgroundImage);

    var valLabel = Titanium.UI.createLabel({
    text:'Error in field (see red text)',
    color:'#fff',
    width:205,
    height:'auto',
    font: {
    fontFamily:'Helvetica Neue',
    fontSize:13,
    fontWeight:'bold'
    },
    textAlign:'center'
    });

    valView.add(valLabel);

    var anim_out = Titanium.UI.createAnimation();
    anim_out.opacity=0;
    anim_out.duration = 4000;
    function validationMessages(){

    //*** 'me' acts as an alias that can be used within the methods
    var me = this;
    var imgPath=Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,"images/bubble.png");

    validationMessages.totalErrors =0;
    validationMessages.reqdfieldsRemaining=0;

    this.messageWin = Titanium.UI.createWindow({
    height: 50,
    width:310,
    top:40,
    touchEnabled:false,
    visible:true,
    zIndex: 999,
    orientationModes : [
    Titanium.UI.PORTRAIT
    ]
    });

    this.valView = Titanium.UI.createView({
    width:310,
    height:50,
    //backgroundColor:'Red',
    //borderRadius:10,
    opacity:1.0,
    touchEnabled:false,
    visible:false,
    backgroundImage: imgPath.nativePath
    //left:35
    });

    this.valLabel = Titanium.UI.createLabel({
    text:'Missing or error in field (see red text)',
    color:'#fff',
    width:300,
    height:50,
    top:2,
    font: {
    fontFamily:'Helvetica Neue',
    fontSize:16,
    fontWeight:'bold'
    },
    textAlign:'center'
    });

    this.valView.add(this.valLabel);

    this.messageWin.add(this.valView);

    this.anim_out = Titanium.UI.createAnimation();
    this.anim_out.opacity=0;
    this.anim_out.duration = 4000;

    //function to display view
    validationMessages.prototype.displayValErr = function displayValErr()
    {
    this.valView.opacity = 1.0;
    this.valView.visible = true;
    this.valView.show();
    this.messageWin.open();
    this.valView.animate(this.anim_out);
    // setTimeout(function()
    // {
    // //for fading out error tip and closing
    // this.valView.hide();
    // //messageWin.close({opacity:0,duration:500});
    // },4000);
    }
    }

    //test is Valid Number => returns bool
    function isValidNumber(val) {
    @@ -49,6 +86,19 @@ function isInteger(val) {
    return re.test(val);
    }

    //function to test double **** maynot be correct ***
    function isDouble(val){
    var re = new RegExp("[0-9.]");
    Titanium.API.debug('isDouble:' + re.test(val));
    return re.test(val);
    }

    function isNumber(val) { // REAL NUMBERS
    var re = new RegExp("/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/");
    Titanium.API.debug('isNumber:' + re.test(val));
    return re.test(val);
    }

    //test for real val
    function isReal(val) {
    var re =/^[-+]?\d*\.?\d+$/;
    @@ -119,20 +169,23 @@ function checkValidation(obj) {
    obj.color = obj.validation.color;

    //keep record of validation colors
    if(!obj.validation.color)
    if(!obj.validation.color){
    obj.validation.color = obj.color;

    obj.validation.backgroundColor = obj.backgroundColor;
    }
    //set valuation highlight effect
    function setEffect(obj,isOff) {
    if (isValid===false) {
    return false;
    } else {
    if(!isOff) {
    obj.color = 'Red';
    obj.backgroundColor = 'Red';
    isValid = false;
    }
    if(isOff) {
    obj.color = obj.validation.color;
    obj.backgroundColor = obj.validation.backgroundColor;
    isValid = true;
    }
    }
    @@ -148,21 +201,27 @@ function checkValidation(obj) {
    if(isPresent(obj.value)) {
    //check for double value
    if(obj.validation.isdouble) {
    setEffect(obj,isReal(obj.value));
    }
    setEffect(obj,isDouble(obj.value));
    };
    //check if need integer
    if(obj.validation.isinteger) {
    if (!setEffect(obj,isInteger(obj.value)))
    obj.value = removeLastEntry(obj.value);
    setEffect(obj,isInteger(obj.value));
    // if (!setEffect(obj,isInteger(obj.value)))
    // obj.value = removeLastEntry(obj.value);
    };
    //check if need min
    if(obj.validation.minchars) {
    setEffect(obj,isMinChars(obj.value,obj.validation.minchars));
    };
    //check if max
    if(obj.validation.maxchars) {
    if(!setEffect(obj,isWithinMaxChars(obj.value,obj.validation.maxchars)))
    obj.value = removeLastEntry(obj.value);
    setEffect(obj,isWithinMaxChars(obj.value,obj.validation.maxchars));
    // removed next cos the check still returns false ....
    // if(!isWithinMaxChars(obj.value,obj.validation.maxchars))
    // {
    // obj.value = removeLastEntry(obj.value);
    // checkValidation(obj);
    // }
    };
    //check within range
    if(obj.validation.range) {
    @@ -172,142 +231,4 @@ function checkValidation(obj) {

    Ti.API.info('isValid:' + isValid);
    return isValid;
    };
    /// UNTEST STUFF BELOW

    // //test valid date => returns bool
    // function isValidDate (dateStr, format) {
    // if (format == null) {
    // format = "MDY";
    // }
    // format = format.toUpperCase();
    // if (format.length != 3) {
    // format = "MDY";
    // }
    // if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || _
    // (format.indexOf("Y") == -1) ) {
    // format = "MDY";
    // }
    // if (format.substring(0, 1) == "Y") { // If the year is first
    // var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
    // var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
    // } else if (format.substring(1, 2) == "Y") { // If the year is second
    // var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
    // var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
    // } else { // The year must be third
    // var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
    // var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
    // }
    // // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
    // if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) {
    // return false;
    // }
    // var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
    // // Check to see if the 3 parts end up making a valid date
    // if (format.substring(0, 1) == "M") {
    // var mm = parts[0];
    // } else _
    // if (format.substring(1, 2) == "M") {
    // var mm = parts[1];
    // } else {
    // var mm = parts[2];
    // }
    // if (format.substring(0, 1) == "D") {
    // var dd = parts[0];
    // } else _
    // if (format.substring(1, 2) == "D") {
    // var dd = parts[1];
    // } else {
    // var dd = parts[2];
    // }
    // if (format.substring(0, 1) == "Y") {
    // var yy = parts[0];
    // } else _
    // if (format.substring(1, 2) == "Y") {
    // var yy = parts[1];
    // } else {
    // var yy = parts[2];
    // }
    // if (parseFloat(yy) <= 50) {
    // yy = (parseFloat(yy) + 2000).toString();
    // }
    // if (parseFloat(yy) <= 99) {
    // yy = (parseFloat(yy) + 1900).toString();
    // }
    // var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
    // if (parseFloat(dd) != dt.getDate()) {
    // return false;
    // }
    // if (parseFloat(mm)-1 != dt.getMonth()) {
    // return false;
    // }
    // return true;
    // }
    //
    // //test valid email address => returns bool
    // function isValidEmail(emailAddress) {
    // var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    // return re.test(emailAddress);
    // }
    //

    //
    // //test is alpha
    // function isAlpha(val) {
    // var re = /^[a-z ._-]+$/i
    // return re.test(val);
    // }
    //
    // //test alphas numercial
    // function isAlphaNum(val) {
    // var re = /^[a-z0-9 ._-]+$/i
    // return re.test(val);
    // }

    // //test for phone
    // function isPhone(val) {
    // var re = /^[\d\s ().-]+$/
    // return re.test(val);
    // }
    //

    // //test for url
    // function isUrl(val) {
    // var re = /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i
    // return re.test(val);
    // }

    // //test valid time => return bool
    // // valid entries 12:25 & 12:25PM
    // function isValidTime(value) {
    // var hasMeridian = false;
    // var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
    // if (!re.test(value)) {
    // return false;
    // }
    // if (value.toLowerCase().indexOf("p") != -1) {
    // hasMeridian = true;
    // }
    // if (value.toLowerCase().indexOf("a") != -1) {
    // hasMeridian = true;
    // }
    // var values = value.split(":");
    // if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) {
    // return false;
    // }
    // if (hasMeridian) {
    // if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) ) {
    // return false;
    // }
    // }
    // if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) {
    // return false;
    // }
    // if (values.length > 2) {
    // if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) {
    // return false;
    // }
    // }
    // return true;
    // }
    //
    };
  3. @coomsie coomsie revised this gist May 12, 2011. 1 changed file with 284 additions and 117 deletions.
    401 changes: 284 additions & 117 deletions validation.js
    Original file line number Diff line number Diff line change
    @@ -1,146 +1,313 @@
    //UI for valiation
    var imgPath=Ti.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory,"images/bubble.png");

    var valView = Titanium.UI.createView({
    width:205,
    height:30,
    backgroundColor:'Red',
    borderRadius:10,
    opacity:0.7,
    touchEnabled:false,
    visible:false,
    //backgroundImage: "images/bubble.png",
    top:2,
    left:50
    });

    Ti.API.info(valView.backgroundImage);

    var valLabel = Titanium.UI.createLabel({
    text:'Error in field (see red text)',
    color:'#fff',
    width:205,
    height:'auto',
    font: {
    fontFamily:'Helvetica Neue',
    fontSize:13,
    fontWeight:'bold'
    },
    textAlign:'center'
    });

    valView.add(valLabel);

    var anim_out = Titanium.UI.createAnimation();
    anim_out.opacity=0;
    anim_out.duration = 4000;

    //test is Valid Number => returns bool
    function isValidNumber(val) {
    var re=/^[-+]?\d+(\.\d+)?$/;
    Titanium.API.debug('isValidNumber:' + re.test(val));
    return re.test(val);
    var re=/^[-+]?\d+(\.\d+)?$/;
    Titanium.API.debug('isValidNumber:' + re.test(val));
    return re.test(val);
    };

    //test for integer
    function isInteger(val) {
    var re= /^[-+]?\d+$/;
    Titanium.API.debug('isInteger:' + re.test(val));
    return re.test(val);
    var re= /^[-+]?\d+$/;
    Titanium.API.debug('isInteger:' + re.test(val));
    return re.test(val);
    }

    //test for real val
    function isReal(val) {
    var re =/^[-+]?\d*\.?\d+$/;
    Titanium.API.debug('isReal:' + re.test(val));
    return re.test(val);
    var re =/^[-+]?\d*\.?\d+$/;
    Titanium.API.debug('isReal:' + re.test(val));
    return re.test(val);
    }



    function removeLastEntry(val) {
    Titanium.API.debug('removeLastEntry:');
    return val.slice(0,val.toString().length-1);
    Titanium.API.debug('removeLastEntry:');
    return val.slice(0,val.toString().length-1);
    };

    //test for empty
    // check to see if input is whitespace only or empty
    function isEmpty( val ) {
    if ( null === val || "" === val ) {
    return true;
    }
    return false;
    if ( null === val || "" === val ) {
    return true;
    } else {
    return false;
    }
    }

    //test if reqd
    function isPresent(val) {
    var re = /[^.*]/;
    Titanium.API.debug('isPresent:' + re.test(val));
    return re.test(val);
    var re = /[^.*]/;
    Titanium.API.debug('isPresent:' + re.test(val));
    return re.test(val);
    }

    //check for value in range
    // check to see if value is within min and max
    function isWithinRange(val, min, max) {
    if (val >= min && val <= max) {
    Titanium.API.debug('isWithinRange:true');
    return true;
    } else {
    Titanium.API.debug('isWithinRange:false');
    return false;
    }
    if (val >= min && val <= max) {
    Titanium.API.debug('isWithinRange:true');
    return true;
    } else {
    Titanium.API.debug('isWithinRange:false');
    return false;
    }
    }

    // check to see if value is within min chars
    function isMinChars(val, min) {
    if (val.toString().length >= min) {
    Titanium.API.debug('isMinChars:true');
    return true;
    } else {
    Titanium.API.debug('isMinChars:false');
    return false;
    }
    if (val.toString().length >= min) {
    Titanium.API.debug('isMinChars:true');
    return true;
    } else {
    Titanium.API.debug('isMinChars:false');
    return false;
    }
    }

    // check to see if value is within max chars
    function isWithinMaxChars(val, max) {
    if (val.toString().length <= max) {
    Titanium.API.debug('isWithinMaxChars:true');
    return true;
    } else {
    Titanium.API.debug('isWithinMaxChars:false');
    return false;
    }
    if (val.toString().length <= max) {
    Titanium.API.debug('isWithinMaxChars:true');
    return true;
    } else {
    Titanium.API.debug('isWithinMaxChars:false');
    return false;
    }
    }


    var isValid=null;


    function checkValidation(obj) {
    Titanium.API.info('checking validation');
    //clear validation
    obj.color = obj.validation.color;

    //keep record of validation colors
    if(!obj.validation.color)
    obj.validation.color = obj.color;


    //set valuation highlight effect
    function setEffect(obj,isOff)
    {
    if (isValid===false)
    {
    return false;
    }else
    {
    if(!isOff)
    obj.color = 'Red';
    isValid = false;
    if(isOff)
    obj.color = obj.validation.color;
    isValid = true;
    }
    return isOff;
    };

    //check if reqd
    if(obj.validation.reqd)
    {
    setEffect(obj,isPresent(obj.value));
    };

    ///validation checks only if Value Present
    if(isPresent(obj.value))
    {
    //check for double value
    if(obj.validation.isdouble) {
    if (!setEffect(obj,isReal(obj.value)))
    setEffect(obj,isReal(obj.value));
    }
    //check if need integer
    if(obj.validation.isinteger) {
    if (!setEffect(obj,isInteger(obj.value)))
    obj.value = removeLastEntry(obj.value);
    };
    //check if need min
    if(obj.validation.minchars) {
    setEffect(obj,isMinChars(obj.value,obj.validation.minchars));
    };
    //check if max
    if(obj.validation.maxchars) {
    if(!setEffect(obj,isWithinMaxChars(obj.value,obj.validation.maxchars)))
    obj.value = removeLastEntry(obj.value);
    };
    //check within range
    if(obj.validation.range)
    {
    setEffect(obj,isWithinRange(obj.value,obj.validation.range.min,obj.validation.range.max));
    };
    };

    Ti.API.info('isValid:' + isValid);

    };
    var isValid=null;
    Titanium.API.info('checking validation');
    //clear validation
    obj.color = obj.validation.color;

    //keep record of validation colors
    if(!obj.validation.color)
    obj.validation.color = obj.color;

    //set valuation highlight effect
    function setEffect(obj,isOff) {
    if (isValid===false) {
    return false;
    } else {
    if(!isOff) {
    obj.color = 'Red';
    isValid = false;
    }
    if(isOff) {
    obj.color = obj.validation.color;
    isValid = true;
    }
    }
    return isOff;
    };

    //check if reqd
    if(obj.validation.reqd) {
    setEffect(obj,isPresent(obj.value));
    };

    ///validation checks only if Value Present
    if(isPresent(obj.value)) {
    //check for double value
    if(obj.validation.isdouble) {
    setEffect(obj,isReal(obj.value));
    }
    //check if need integer
    if(obj.validation.isinteger) {
    if (!setEffect(obj,isInteger(obj.value)))
    obj.value = removeLastEntry(obj.value);
    };
    //check if need min
    if(obj.validation.minchars) {
    setEffect(obj,isMinChars(obj.value,obj.validation.minchars));
    };
    //check if max
    if(obj.validation.maxchars) {
    if(!setEffect(obj,isWithinMaxChars(obj.value,obj.validation.maxchars)))
    obj.value = removeLastEntry(obj.value);
    };
    //check within range
    if(obj.validation.range) {
    setEffect(obj,isWithinRange(obj.value,obj.validation.range.min,obj.validation.range.max));
    };
    };

    Ti.API.info('isValid:' + isValid);
    return isValid;
    };
    /// UNTEST STUFF BELOW

    // //test valid date => returns bool
    // function isValidDate (dateStr, format) {
    // if (format == null) {
    // format = "MDY";
    // }
    // format = format.toUpperCase();
    // if (format.length != 3) {
    // format = "MDY";
    // }
    // if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || _
    // (format.indexOf("Y") == -1) ) {
    // format = "MDY";
    // }
    // if (format.substring(0, 1) == "Y") { // If the year is first
    // var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
    // var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
    // } else if (format.substring(1, 2) == "Y") { // If the year is second
    // var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
    // var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
    // } else { // The year must be third
    // var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
    // var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
    // }
    // // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
    // if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) {
    // return false;
    // }
    // var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
    // // Check to see if the 3 parts end up making a valid date
    // if (format.substring(0, 1) == "M") {
    // var mm = parts[0];
    // } else _
    // if (format.substring(1, 2) == "M") {
    // var mm = parts[1];
    // } else {
    // var mm = parts[2];
    // }
    // if (format.substring(0, 1) == "D") {
    // var dd = parts[0];
    // } else _
    // if (format.substring(1, 2) == "D") {
    // var dd = parts[1];
    // } else {
    // var dd = parts[2];
    // }
    // if (format.substring(0, 1) == "Y") {
    // var yy = parts[0];
    // } else _
    // if (format.substring(1, 2) == "Y") {
    // var yy = parts[1];
    // } else {
    // var yy = parts[2];
    // }
    // if (parseFloat(yy) <= 50) {
    // yy = (parseFloat(yy) + 2000).toString();
    // }
    // if (parseFloat(yy) <= 99) {
    // yy = (parseFloat(yy) + 1900).toString();
    // }
    // var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
    // if (parseFloat(dd) != dt.getDate()) {
    // return false;
    // }
    // if (parseFloat(mm)-1 != dt.getMonth()) {
    // return false;
    // }
    // return true;
    // }
    //
    // //test valid email address => returns bool
    // function isValidEmail(emailAddress) {
    // var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(2([0-4]\d|5[0-5])|1?\d{1,2})(\.(2([0-4]\d|5[0-5])|1?\d{1,2})){3} \])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    // return re.test(emailAddress);
    // }
    //

    //
    // //test is alpha
    // function isAlpha(val) {
    // var re = /^[a-z ._-]+$/i
    // return re.test(val);
    // }
    //
    // //test alphas numercial
    // function isAlphaNum(val) {
    // var re = /^[a-z0-9 ._-]+$/i
    // return re.test(val);
    // }

    // //test for phone
    // function isPhone(val) {
    // var re = /^[\d\s ().-]+$/
    // return re.test(val);
    // }
    //

    // //test for url
    // function isUrl(val) {
    // var re = /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i
    // return re.test(val);
    // }

    // //test valid time => return bool
    // // valid entries 12:25 & 12:25PM
    // function isValidTime(value) {
    // var hasMeridian = false;
    // var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
    // if (!re.test(value)) {
    // return false;
    // }
    // if (value.toLowerCase().indexOf("p") != -1) {
    // hasMeridian = true;
    // }
    // if (value.toLowerCase().indexOf("a") != -1) {
    // hasMeridian = true;
    // }
    // var values = value.split(":");
    // if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) {
    // return false;
    // }
    // if (hasMeridian) {
    // if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) ) {
    // return false;
    // }
    // }
    // if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) {
    // return false;
    // }
    // if (values.length > 2) {
    // if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) {
    // return false;
    // }
    // }
    // return true;
    // }
    //
  4. @coomsie coomsie revised this gist May 5, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion validation.js
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ function isEmpty( val ) {

    //test if reqd
    function isPresent(val) {
    var re = /[^.*]/
    var re = /[^.*]/;
    Titanium.API.debug('isPresent:' + re.test(val));
    return re.test(val);
    }
  5. @coomsie coomsie created this gist May 5, 2011.
    146 changes: 146 additions & 0 deletions validation.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,146 @@
    //test is Valid Number => returns bool
    function isValidNumber(val) {
    var re=/^[-+]?\d+(\.\d+)?$/;
    Titanium.API.debug('isValidNumber:' + re.test(val));
    return re.test(val);
    };

    //test for integer
    function isInteger(val) {
    var re= /^[-+]?\d+$/;
    Titanium.API.debug('isInteger:' + re.test(val));
    return re.test(val);
    }

    //test for real val
    function isReal(val) {
    var re =/^[-+]?\d*\.?\d+$/;
    Titanium.API.debug('isReal:' + re.test(val));
    return re.test(val);
    }


    function removeLastEntry(val) {
    Titanium.API.debug('removeLastEntry:');
    return val.slice(0,val.toString().length-1);
    };

    //test for empty
    // check to see if input is whitespace only or empty
    function isEmpty( val ) {
    if ( null === val || "" === val ) {
    return true;
    }
    return false;
    }

    //test if reqd
    function isPresent(val) {
    var re = /[^.*]/
    Titanium.API.debug('isPresent:' + re.test(val));
    return re.test(val);
    }

    //check for value in range
    // check to see if value is within min and max
    function isWithinRange(val, min, max) {
    if (val >= min && val <= max) {
    Titanium.API.debug('isWithinRange:true');
    return true;
    } else {
    Titanium.API.debug('isWithinRange:false');
    return false;
    }
    }

    // check to see if value is within min chars
    function isMinChars(val, min) {
    if (val.toString().length >= min) {
    Titanium.API.debug('isMinChars:true');
    return true;
    } else {
    Titanium.API.debug('isMinChars:false');
    return false;
    }
    }

    // check to see if value is within max chars
    function isWithinMaxChars(val, max) {
    if (val.toString().length <= max) {
    Titanium.API.debug('isWithinMaxChars:true');
    return true;
    } else {
    Titanium.API.debug('isWithinMaxChars:false');
    return false;
    }
    }


    var isValid=null;

    function checkValidation(obj) {
    Titanium.API.info('checking validation');
    //clear validation
    obj.color = obj.validation.color;

    //keep record of validation colors
    if(!obj.validation.color)
    obj.validation.color = obj.color;


    //set valuation highlight effect
    function setEffect(obj,isOff)
    {
    if (isValid===false)
    {
    return false;
    }else
    {
    if(!isOff)
    obj.color = 'Red';
    isValid = false;
    if(isOff)
    obj.color = obj.validation.color;
    isValid = true;
    }
    return isOff;
    };

    //check if reqd
    if(obj.validation.reqd)
    {
    setEffect(obj,isPresent(obj.value));
    };

    ///validation checks only if Value Present
    if(isPresent(obj.value))
    {
    //check for double value
    if(obj.validation.isdouble) {
    if (!setEffect(obj,isReal(obj.value)))
    setEffect(obj,isReal(obj.value));
    }
    //check if need integer
    if(obj.validation.isinteger) {
    if (!setEffect(obj,isInteger(obj.value)))
    obj.value = removeLastEntry(obj.value);
    };
    //check if need min
    if(obj.validation.minchars) {
    setEffect(obj,isMinChars(obj.value,obj.validation.minchars));
    };
    //check if max
    if(obj.validation.maxchars) {
    if(!setEffect(obj,isWithinMaxChars(obj.value,obj.validation.maxchars)))
    obj.value = removeLastEntry(obj.value);
    };
    //check within range
    if(obj.validation.range)
    {
    setEffect(obj,isWithinRange(obj.value,obj.validation.range.min,obj.validation.range.max));
    };
    };

    Ti.API.info('isValid:' + isValid);

    };