Skip to content

Instantly share code, notes, and snippets.

@danielfreitasce
Last active December 15, 2022 11:35
Show Gist options
  • Save danielfreitasce/b3340236be71ed8831d645a596d6d538 to your computer and use it in GitHub Desktop.
Save danielfreitasce/b3340236be71ed8831d645a596d6d538 to your computer and use it in GitHub Desktop.
Some validations
function isEmptyParam(param) {
if (param || param.length > 0)
return false;
else
return true;
}
function verifyMinCaracteres(min, str) {
if (str.length >= min)
return true;
else
return false;
}
function verifyMaxCaracteres(max, str) {
if (str.length === max)
return true;
else
return false;
}
function isInteger(num) {
if (Number.isInteger(num))
return true;
else
return false;
}
function isRealNumber(num) {
if (!Number.isInteger(num) && isReal(num))
return true;
else
return false;
}
function isReal(num) {
return typeof num == 'number' && !isNaN(num);
}
console.log(`100 is real number : ${isReal(100)}`);
console.log(`100.2334532322333 is real number: ${isRealNumber(100.2334532322333)}`);
console.log(`100 is intenger: ${isInteger(100)}`);
console.log(`100.122345 is intenger: ${isInteger(100.122345)}`);
console.log(`'Olá mundo!' has 10 characters: ${verifyMaxCaracteres(10, 'Olá mundo!')}`);
console.log(`'Hello' has at least 3 characters: ${verifyMinCaracteres(3, 'Hello')}`);
console.log(`'Oi' has at least 3 characters: ${verifyMinCaracteres(3, 'Oi')}`);
console.log(`'' is a empty string: ${isEmptyParam('')}`);
console.log(`'Olá mundo!' is a empty string: ${isEmptyParam('Olá mundo!')}`);
console.log(`100 is a empty param: ${isEmptyParam(100)}`);
console.log(`null is a empty param: ${isEmptyParam(null)}`);
console.log(`undefined is a empty param: ${isEmptyParam()}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment