Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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.
Reply With Quote
  #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
Default

Hi Everest!!
Hope this will help you :)
<script>
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;
  temp_original=theValue;
  var arr=theValue.split("|");
   toTheLeft=arr[0].split(",")[arr[0].split(",").length-1];
  toTheRight = arr[1].split(',')[0];
    if(toTheLeft.indexOf(".")>=0)
        {
         dec_right=temp_original.indexOf(".")
         dec_left=temp_original.substring(dec_right+1,temp_ original.length)
         dec_left_length=dec_left.length
                    if(event.keyCode!=46&&dec_left_length<((precision2 2)+1))
                            { event.returnValue = true;}
                       else
                           { event.returnValue = false;}
       }

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

 }


 </script>

</head>

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

_____________************_______________
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
http://www.experts-exchange.com/Prog..._20739343.html


Cheers :)

vinod
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:47 PM.


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