Skip to content

Instantly share code, notes, and snippets.

@roe3p
Created May 18, 2016 11:11
Show Gist options
  • Save roe3p/7aa084cc45858ecd0be4a7a40b9fc666 to your computer and use it in GitHub Desktop.
Save roe3p/7aa084cc45858ecd0be4a7a40b9fc666 to your computer and use it in GitHub Desktop.
A5 Field validation functions
//''------------------------- Validation Functions
function isValidDate(s) {
var bits = s.split('/');
var d = new Date(bits[2], bits[1] - 1, bits[0]);
return d && (d.getMonth() + 1) == bits[1] && d.getDate() == Number(bits[0]);
}
function validate(data, type, maxLen, req){
//debugger;
req = (typeof(req) == 'undefined') ? true : req;
//check for correct datatype
switch(type) {
case 'date':
if(!isValidDate(data)){
return getMessage('VAL_INVALIDDATE');
}
}
//check for empty value
if(req == true && (data == '' || data == null)){
return getMessage('VAL_EMPTY');
}
//check for max length
if(typeof(maxLen) == 'number'){
if(data.length > maxLen)
return getMessage('VAL_TOOLONG').replace('{length}', maxLen);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment