Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7959493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:34:57+00:00 2026-06-04T04:34:57+00:00

hey guys should be a easy one…I have some javascript that is turning my

  • 0

hey guys should be a easy one…I have some javascript that is turning my input values into currency values. Problem is it will fail if I try to type in .5 heres is my code:

function handleCurrency(formName,fieldName)
{
    var enteredValue = document.forms[formName].elements[fieldName].value;

    if ( enteredValue.isCurrency() )
    {
        alert("This is currency " + enteredValue )
        // Put the nicely formatted back into the text box.
        document.forms[formName].elements[fieldName].value = enteredValue.toCurrency();
    }
}

jsp:

 <td><input type="text" name="replacementCost" onchange="handleCurrency('NsnAdd','replacementCost')" value="<ctl:currencyFormat currency='${form.replacementCost}'/>" onkeypress="javascript:return noenter();" <c:if test="${!lock.locked}">disabled="disabled"</c:if> /></td>

How can I make it so that .5 is allowable also to be formatted?

custom javascript:

var patternWithCommas = new RegExp("^\\s*\\$?-?(\\d{1,3}){1}(,\\d{3}){0,}(\\.\\d{1,2})?\\s*$");
var patternWithoutCommas = new RegExp("^\\s*\\$?-?\\d+(\\.\\d{1,2})?\\s*$");

function stringIsCurrency()
{
if (patternWithoutCommas.test(this))
{
    return true;
}
else if (patternWithCommas.test(this))
{
    return true;
}
return false;
}

function stringToCurrency()
{
if (this == '') return this;

var str = this.replace(/[$,]+/g,'');

sign = (str == (str = Math.abs(str)));
str = Math.floor(str*100+0.50000000001);
cents = str%100;
str = Math.floor(str/100).toString();

if (cents<10) cents = "0" + cents;

for (var i = 0; i < Math.floor((str.length-(1+i))/3); i++)
{
    str = str.substring(0,str.length-(4*i+3))+','+
          str.substring(str.length-(4*i+3));
}

str = '$' + ((sign)?'':'-') + str + '.' + cents;

return str;
}

String.prototype.isCurrency = stringIsCurrency;
String.prototype.toCurrency = stringToCurrency;

basically it needs to allow .5 and not just 0.5

this needs to be updated:

var patternWithCommas = new RegExp("^\\s*\\$?-?(\\d{1,3}){1}(,\\d{3}){0,}(\\.\\d{1,2})?\\s*$"); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:34:58+00:00Added an answer on June 4, 2026 at 4:34 am

    You have not shown your code for isCurrency.

    Here’s how I would do it:

    function isCurrency( val )
    {
        return /^\$?(?:\d[\d,]*)?(?:.\d\d?)?$/.test( val );
    }
    

    See it here in action: http://regexr.com?3103a


    Now that you have provided your code, here’s my proposed solution.

    While there are many things I would have done differently,
    in order to keep the spirit of your code, just change this:

    var patternWithCommas = new RegExp("^\\s*\\$?-?(\\d{1,3}){1}(,\\d{3}){0,}(\\.\\d{1,2})?\\s*$");
    var patternWithoutCommas = new RegExp("^\\s*\\$?-?\\d+(\\.\\d{1,2})?\\s*$");
    

    to this:

    var patternWithCommas = /^\s*\$?-?((\d{1,3}){1}(,\d{3})?)?(\.\d{1,2})?\s*$/;
    var patternWithoutCommas = /^\s*\$?-?(\d+)?(\.\d{1,2})?\s*$/;
    

    which would make the dollar amount optional.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys, unusual question: I have a input field #birthday that should accept european
hey guys, I have an input field that looks for matched characters on a
Hey guys I have what should be a simple question, but I am new
Hey guys quick question, I have an add remove script that will add a
Hey guys really quick question, I have a simple test that I was doing
Hey guys I have my own webserver that is hosting a website that I
Hey guys, At some point i think that these stm implementation ( multiverse which
Hey guys! I have this little problem: I have one ViewController which adds 2
hey guys, i spent some time on this one but i find it really
Hey guys, I have an interesting one for you here! I have one object,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.