Input - Set Min and Max value

Input - Set Min and Max value

  

Hi,


I have a requirement that a value X, bind to an input, must be between 0 - 100.

I can do this by using the OnChange event but do you know a better way? Client side javascript can be an option?


Thank you for your opinions.

Hi Filipe,

You can use something like this :

$('input').on('input', function () {
   
    var value = $(this).val();
   
    if (value !== '') {
       
        $(this).val(Math.max(Math.min(value, 100), 0));
    }
});

If you use this on only 1 place you can use it on a expression and pass the id of the input. If you use in more classes you can use a class.

Regards,

Marcelo

Hi Filipe,

If you go with a javascript validation, it's a good practice to also always perform the validation on the server side. The user can always disable javascript on his browser or tamper with the javascript code using the browser's developer tools.

Hi Filipe, 

Aurelio is right you can use the jquery so your code doesnt need to go to the server everytime the user changes the input but in the end when you submit the values  or you use the value on that input you should always perform the validations again.

Regards,

Marcelo