How do I force certain input fields in my form to accept only positive values with up to two decimal points?

I am using jquery calx to create a form that works out several company expenses for an ecommerce site.
In some of the fields - all those with class checkNegative (or marked with #A2#, #A18#, #A19#, #A20#, #A28# - I would like to force users to insert only positive values with up to two decimal places.

How do I do it?

I have tried using this function but it sort of does not work.
$(".checkNegative").bind("change paste input", function(){
	$(this).val( Math.abs($(this).val()) );
});				

Open in new window

With this code I cannot enter any negative values because as soon as I try it gives me NaN error (whereas I would like like to remove the "-" sign when typed). However I am unable to add any numbers that are not whole (like with decimal places)

Please help

thanks in advance

I am attaching the code for testing.
calx-formulae.zip
badwolfffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
Here you go: http://jsbin.com/qapequ/1/edit?js,output

$(".checkNegative").on("input", function(e){
	var t = isNaN(Number(e.target.value)) ? 0 : e.target.value;
    $(e.target).val(Math.abs(Number(t)));
});

Open in new window

0
badwolfffAuthor Commented:
Hi,

it doesn't work right. When I try to add a normal positive number like 11.55 it forces me to take it as 1155.
I need to be able to put in

11
11.55
11.5 (this already becomes 11.50)

if instead I were to put in

-11 > becomes > 11
-11.55 > becomes > 11.55
-11.5 > becomes > 11.50

there is another problem with your function. If you try to add any values to A18 - A20, I get strange numbers automatically in A28 (while it should not change at all)

I think we need a Regex here but I don't know how to write it.

thanks
0
badwolfffAuthor Commented:
I managed it myself

thanks anyways

$("#valoredonazione").bind("change paste input", function () {
    if ($(this).val() < 0) {
        window.console && console.log('condition met');
        $(this).val(function (index, value) {
            return value.replace('-', '');
        });
    }
});

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

RobOwner (Aidellio)Commented:
Cool. Glad you got your answer :)
0
Michel PlungjanIT ExpertCommented:
Or just

$("#valoredonazione").on("change paste input", function () {
   var val = $(this).val();
    if (parseFloat(val) < 0) {
        window.console && console.log('condition met');
        $(this).val(val.replace('-', ''));
    }
});

Open in new window

0
badwolfffAuthor Commented:
I solved it myself
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.