Click here to Skip to main content
14,775,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Not knowing enough about javascript I tried to make changes to the code but it didn't work. Can anyone please help identify for me where I can expand the field to accept amounts more than $123. I should be able to see $1234.00 or $12345.

What I have tried:

https://***REDACTED***/buynow.html is the page that is not working correctly.


Here is the code:

function totalamountdue() {
    var inputs = document.getElementsByTagName("INPUT");
    var runningTotal = 0;
    for (i = 0; i < inputs.length; i++) {
        if (inputs[i].id == "total") { inputs[i].value = runningTotal }
        if (inputs[i].id.indexOf("amount") == 0) {
            if (/^[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(inputs[i].value)) {
                runningTotal += +inputs[i].value;
            } else {
                if (inputs[i].value == "THIS IS A BAD VALUE") { inputs[i].value = ""; }
                if (inputs[i].value) { inputs[i].value = "THIS IS A BAD VALUE"; }
            }
        }

    }
}
Posted
Updated 5-May-20 11:58am
v3

You could try to replace
if (/^[1-9][0-9]{0,2}(,[0-9]{3})*(\.[0-9]{2})?$/.test(inputs[i].value))
with
if (!isNaN(parseFloat(inputs[i].value)) && isFinite(inputs[i].value))
Using a regular expression to validate a number is a terrible choice.
   
Comments
esebagel 5-May-20 16:36pm
   
You are amazing! Thank you so much! It worked flawlessly!
phil.o 5-May-20 16:42pm
   
You're welcome. Please mark your question as answered if it's the case.
And please, do not put links in your questions like you did; you could be flagged as spammer over this. I redacted the link to avoid that.
Quote:
I have an amount field being limited to only 3 digits.

If you study RegEx, you will see that input is not limited to 3 digits values, instead of 12345 , it accept 12.345 .

Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900