Solved

Javascript to allow only decimals

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
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

737 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