We help IT Professionals succeed at work.

Decimal validation

qader99
qader99 asked
on
I have an amount field in the database as (15,2) ie. it would accept 13 figures beforw the decimal point with the precesion of 2.

I have set the maxlength of the text field to 16.
I want to do  javascript validation on submit of the form so that

1. No one can type all fifteen figures without the decimal point. Max allowed should be thirteen.
2. No one can type more than two figures after the decimal point. Max allowed should be two.

I require the above two validation as not confirming to above causes my insert or update query to fail.

Thanks in advance
Comment
Watch Question

Commented:
Call this in your onsubmit validation function:

function validateFormattedNum(sTxt) {
 var regex = /\d{1,13}\.\d{2}/;
 return regex.test(sTxt);
}

Enjoy!
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
Make sure you test on the server too

It is very tempting for people to mess with your server by turning js off or rewrite the form

Michel

Explore More ContentExplore courses, solutions, and other research materials related to this topic.