JavaScript code to include Maximum and minimum limit for a number Z -


function add(id,value) { var x  = document. element id(id). value ; x = x.replace('$',''); x  = x.replace(',',''); var y = value; var z = +x + +y;     document.get element id(id). value =z; } 

to set minimum value z 0 , maximium value 999999999

if question how make sure z never less 0 or greater 999999999, there 2 common ways:

  1. using if:

    if (z < 0) {     z = 0; } else if (z > 999999999) {     z = 999999999; } 
  2. using math:

    z = math.max(0, math.min(z, 999999999)); 

    math.min(z, 999999999) pick smallest of values give it, , won't return value greater 999999999. similarly, math.max(0, ...) return largest of 2 values give it, , won't return value less 0.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -