Skip to content

Instantly share code, notes, and snippets.

@imCoding
Forked from coomsie/validation.js
Created February 9, 2014 14:11
Show Gist options
  • Select an option

  • Save imCoding/8899627 to your computer and use it in GitHub Desktop.

Select an option

Save imCoding/8899627 to your computer and use it in GitHub Desktop.
//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);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment