?
Solved

Javascript to allow only decimals

Posted on 2011-09-22
4
Medium Priority
?
739 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
[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
4 Comments
 
LVL 5

Accepted Solution

by:
zvytas earned 2000 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

Independent Software Vendors: 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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to create an extensible mechanism for linked drop downs.
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 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)
Suggested Courses

770 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