Solved

Javascript to allow only decimals

Posted on 2011-09-22
4
731 Views
Last Modified: 2012-05-12
OK, I'm sure this is easy for someone but I can't seem to find an answer anywhere. I have a textbox on an asp.net page. I need to make sure that anything written in that box is a decimal between 0 and 1 i.e: .110, .56, .034. Values such as -.99, 1.22, etc are not allowed, nor are alpha characters, a % sign or other non-numeric characters (except the decimal point).

Does anyone have any Javascript that validates a user suppiled input is within a certain range? BTW, I'm fine with using a .net validator control instead if someone can give me the regular expression I need for it.

Thanks in advance!  
0
Comment
Question by:BrogansMom
4 Comments
 
LVL 5

Accepted Solution

by:
zvytas earned 500 total points
ID: 36580995
The following Regex should do it:

^\.(\d+)$
0
 
LVL 82

Expert Comment

by:leakim971
ID: 36581212
when do you want to validate?

here onblur : if(isNaN(this.value)||this.value<=0||this.value>=1) this.value='';

test page : http://jsfiddle.net/zArCH/1/
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36581280
In my opinion is this only "lazy" typing but not decimal: .034
Therefore this my proposal:
<... onKeyUp="if(!this.value.match(/^0?(\.\d*)?$/))this.value='0.'+this.value.replace(/\D/g,'');" >

Open in new window

0
 

Author Closing Comment

by:BrogansMom
ID: 36581313
That was exactly the expression I needed!!!

Here's how I implemented it, in case anyone benefits by this thread:
            function DecimalCheck() {
                var cc = $get("<%=rate.ClientID %>");
                debugger;
               
                if (cc.value == '') {
                    alert('Please provide a rate');
                    return false;
                }
               
                var rex = /^\.(\d+)$/
                if (rex.test(cc.value)) {
                    return true;
                }
               
                alert('Please enter the spread amount as a decimal.');
                return false;
            }
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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?
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

713 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