Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 11th, 2008, 03:58 PM
Authorized User
Join Date: Mar 2004
Location: , , .
Posts: 74
Thanks: 0
Thanked 0 Times in 0 Posts
Default reg. exp. for up to 2 decimal places

i need to allow only numbers and up to 2 decimal places. is there a way to do it using reg.exp in javwscript? if you have sample code, that would be great.
  #2 (permalink)  
Old February 11th, 2008, 04:51 PM
Friend of Wrox
Join Date: Oct 2004
Location: delhi, delhi, India.
Posts: 553
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to vinod_yadav1919 Send a message via Yahoo to vinod_yadav1919

Hi Everest!!
Hope this will help you :)
function storeCaret (x) {
 if (!document.all) {return}
  textEl = x
  if (textEl.createTextRange)
     caretPos = document.selection.createRange();


function update (field,event,precision22) {

var isIE = document.all?true:false;
var isNS = document.layers?true:false;
var key = (isIE) ? window.event.keyCode : e.which;
var obj = (isIE) ? event.srcElement : e.target;

if ( (event.keyCode >47&& event.keyCode <58)||event.keyCode ==46)
  var icaretPos = (caretPos) ? caretPos : textEl.length;
  icaretPos.text = "|";
   theValue = textEl.value;
  var arr=theValue.split("|");
  toTheRight = arr[1].split(',')[0];
         dec_left=temp_original.substring(dec_right+1,temp_ original.length)
                    if(event.keyCode!=46&&dec_left_length<((precision2 2)+1))
                            { event.returnValue = true;}
                           { event.returnValue = false;}

        if((toTheRight.indexOf(".")>=0)&&event.keyCode ==46)
                     {event.returnValue = false;}
     caretPos.moveStart("character", -1);
  event.returnValue = false;




<body >
<td>only2Digits after decimal<input name="txt" onkeypress="storeCaret(this);update(this,event,2)" ></td>
<tr><td>only5Digits after decimal<input name="txt2" onkeypress="storeCaret(this);update(this,event,5)" ></td>

It will only allow decimal(./dot) ,no other characters.
You can specfiy number of decimal digits
You can enter other number after decimal
You can't put more than one dot(.)

Please a look on the following link too

Cheers :)


Similar Threads
Thread Thread Starter Forum Replies Last Post
Number with 2 decimal places only pallone SQL Server 2000 4 July 3rd, 2007 07:29 AM
Display Decimal Places rsm42 ASP.NET 1.0 and 1.1 Basics 3 January 7th, 2007 10:46 AM
reg exp for password swarnap Javascript How-To 2 October 25th, 2006 02:04 AM
Round to 2 decimal places adman JSP Basics 0 June 30th, 2005 07:24 AM
Two decimal places only pallone Javascript How-To 5 February 2nd, 2005 12:35 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.