Solved

Javascript to allow only decimals

Posted on 2011-09-22
4
722 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now