You are viewing limited content. For full access, please sign in.

Question

Question

Restrict Currency Field to Positive Values?

asked on March 19, 2015

Is there a way using JavaScript to force a currency field to only accept positive values?

0 0

Answer

SELECTED ANSWER
replied on March 19, 2015

One option is to convert the negative number to a positive number. For that, you can use Math.abs(value). If you want the negative number to just be reset to 0, then you can use Math.max(0, value).

$(document).ready(function () {
  $('.absvalue input').change(function() {
    $('.absvalue input').val(Math.abs($(this).val()));
  });
  
  $('.zerovalue input').change(function() {
    $('.zerovalue input').val(Math.max(0,$(this).val()));
  });
});
2 0

Replies

You are not allowed to reply in this post.
replied on March 19, 2015
$(document).ready(function(){
  $("#q10 input").change(function (){
    if ($(this).val() > 0) {
        $("#q10 div").append("<div id='errorbox-2' class='ws-errorbox' style='display: block;'><p class='ws-errormessage'>Value must be less than or equal to $50,000.</p></div>");
        $(this).addClass("user-error");
        $(this).removeClass("user-success");
    } else {
     $("#errorbox-2").remove()
     $(this).removeClass("user-error"); 
     $(this).addClass("user-success");

    }
  });
});

Kinda sloppy but an idea that piggy backs off the existing error displays.

 

I had to build one just now to constraint a currency field to be < 50000 .

replied on March 19, 2015

Bleh, it's still buggy. Will update thread when resolved.

You are not allowed to follow up in this post.

Sign in to reply to this post.