Solved

How to do currency validation that also allowed to put negative at infront.

Posted on 2008-10-13
1
552 Views
Last Modified: 2012-05-05
Hi,

For the below Java sctipt, How can I modify it so that my input will allowed me to put the negative sign "-" at the begining?

<html>
<head>
      <title>Test Page</title>

      <script language="JavaScript">
     
      function main()
      {
            document.getElementById("price").focus();
      }

      function parseCurrency(field)
      {
            var currency = /^\d*(?:\.\d{0,2})?$/;
            var onlyCurrency = /^(\d*(?:\.\d{0,2})?)[\s\S]*$/;
           
            if(!currency.test(field.value))
            {
                  field.value = field.value.replace(onlyCurrency, "$1");
            }
      }
     
      </script>
</head>

<body onload="javascript:main();">

      $ <input type="text" id="price" name="price" onKeyUp="javascript:parseCurrency(this);" onChange="javascript:parseCurrency(this);">

</body>
</html>
0
Comment
Question by:runbird
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
Lolly-Ink earned 200 total points
ID: 22708246
Add the minus sign to the 2 regular expressions:
<html>
<head>
      <title>Test Page</title>
 
      <script language="JavaScript">
      
      function main()
      {
            document.getElementById("price").focus();
      }
 
      function parseCurrency(field)
      {
            var currency = /^\-?\d*(?:\.\d{0,2})?$/;
            var onlyCurrency = /^(\-?\d*(?:\.\d{0,2})?)[\s\S]*$/;
            
            if(!currency.test(field.value))
            {
                  field.value = field.value.replace(onlyCurrency, "$1");
            }
      }
      
      </script>
</head>
 
<body onload="javascript:main();">
 
      $ <input type="text" id="price" name="price" onKeyUp="javascript:parseCurrency(this);" onChange="javascript:parseCurrency(this);">
 
</body>
</html>

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Date on a table 16 33
Javascript Regex 7 20
Any Way to rotate banner ads from Commission Junction? 2 37
Add - in a loop to first and last element. 6 37
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question