I have this code:

isTinyInt: function () {
        var val = $(this).val();
        if (val >= 0 && val <= 255)
            return true;
        else
            return false;
    }



Can I make sure with this that the number is in the tinyint range?

Dani AI

Generated

Short answer: the simple range test will work only when the value is actually a numeric integer. 's comparison (val >= 0 && val <= 255) is fine as a bounds check, but extra validation is needed because $(this).val() returns a string and inputs can be empty, non-numeric, fractional, or given in scientific/hex notation. Building on 's plugin idea, a robust check should (a) verify the value is numeric, (b) verify it is an integer, and (c) apply the 0..255 bounds.

A compact, modern check:

function isUnsignedTinyInt(value) {
  const n = Number(value);
  return Number.isInteger(n) && n >= 0 && n <= 255;
}

A compatible fallback for older environments:

function isUnsignedTinyInt(value) {
  var n = Number(value);
  return !isNaN(n) && n % 1 === 0 && n >= 0 && n <= 255;
}

Notes and pitfalls tied to the thread: 's general plugin is a useful pattern, but avoid testing NaN by direct equality — NaN !== NaN, so use isNaN() or Number.isNaN() after conversion. Also beware that very large SQL ranges (bigint) exceed JavaScript's safe integer range; for those cases use BigInt or server-side checks. For form UX, prefer an HTML5 number input with min/max/step attributes so browsers can help enforce 0..255, but never rely solely on client-side validation.

Final point: client-side checks should be defensive and mirror server-side rules. Always validate the final value on the server before inserting into the database (SQL tinyint unsigned = 0..255).

Recommended Answers

All 3 Replies

signed or unsigned tinyint?

unsigned tinyint

Your function looks good though it will only work if this is an appropriate DOM elememt.

You might like to consider a generalised int verifier framed as a jQuery plugin :

jQuery.fn.isInSqlIntRange = function (options) {
    var settings = $.extend({
        'type': 'int',
        'unsigned': false
    }, options);
    var min, max, val = this.val();
    if (val === '' || Number(val) === NaN) {
        return false;
    }
    val = Number(val);
    switch (settings.type) {
        case 'tinyint':
            min = settings.unsigned ? 0 : -128;
            max = settings.unsigned ? 255 : 127;
            break;
        case 'smallint':
            min = settings.unsigned ? 0 : -32768;
            max = settings.unsigned ? 65535 : 32767;
            break;
        case 'mediumint':
            min = settings.unsigned ? 0 : -8388608;
            max = settings.unsigned ? 16777215 : 8388607;
            break;
        case 'int':
        case 'integer':
            min = settings.unsigned ? 0 : -2147483648;
            max = settings.unsigned ? 4294967295 : 2147483647;
            break;
        case 'bigint':
            min = settings.unsigned ? 0 : -9223372036854775808;
            max = settings.unsigned ? 18446744073709551615 : 9223372036854775807;
            break;
        default:
            throw typeError('jQuery.isInIntRange() - type is undefined or invalid');
    }
    return val >= min && val <= max;
}

Sample call :

var foo = $("#myInput").isInSqlIntRange({
    'type': 'tinyint',
    'unsigned': true
});

DEMO

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.