Validation javascript

I need a validation script .
I have a textfield for amount.
I want user can not type more than 100 in this textfield…also user are not allowed to type more than two decimal.

so user can type from 0 to 99.99

Could you please suggest a solution.

I am looking for a concise easy javascript or jquery solution which could help…

A regular expression (regex) would be your best bet.

This pattern should work for you (disclaimer, I found it elsewhere):

 ^(?=.*[1-9])\d{0,2}(?:\.\d{0,2})?$

You can do this purely in HTML if you wish - take a look at this page and see how you go on.

The decimal places might be a little harder, but take a look at this also

2 Likes

I’m not using HTML 5

What are you using instead?

I found a script in web which seems working as per my need.

Is it possible to modify this code so that user can not type more than 100 ?

I am not sure where to put that restriction in the code…i.e user can type max = 100

so , 0… 99.99 , 100 // all these are valid values.

Solved myself. No problem.

Almost everyone is using browsers that support HTML 5 now. If you are not using it then you will be one of the minority where you are relying on the server side validation. For most people whose browser is using HTML 5 the pattern attribute will pick it up before sending it. More people’s browsers support HTML 5 than have JavaScript turned on so a JavaScript solution instead of an HTML one will just mean more people don’t get it validated in their browser.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.