Problem: How to test/check if a value is an Unsigned Integer (UINT) in JavaScript.
Alternative Answers: Thanks goes to the following for improving this answer!
@jbabey:if Number(val) > 0
@SLaks:if (/^\d+$/.test(someString))
About: This shows how to test/check if a value is an Unsigned Integer (UINT) in JavaScript.
Usage: JS: isUINT( value );
Returns: True OR False
Expanded Version:
<script type="text/javascript">
function isUINT(v)
{
var r = RegExp(/(^[^\-]{0,1})?(^[\d]*)$/);
return r.test(v) && v.length > 0;
}
</script>
Minified Version:
function isUINT(v){var r=RegExp(/(^[^\-]{0,1})?(^[\d]*)$/);return r.test(v)&&v.length>0}
Comments / Alternatives are Welcomed!
if (/^\d+$/.test(someString))
Old question but now you can use
const isUINT = (val) => Number.isInteger(val) && val >= 0
console.log('"a":', isUINT('a'))
console.log('"1":', isUINT('1'))
console.log('null:', isUINT(null))
console.log('-1:', isUINT(-1))
console.log('0:', isUINT(0))
console.log('1:', isUINT(1))
console.log('1.25:', isUINT(1.25))
console.log('0.25:', isUINT(0.25))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With