/    Sign up×
Community /Pin to ProfileBookmark

Regular expression to enforce 2 digits after decimal point

<html>
<script>
function doCheck(txtidval)
{
var i,exceptions=[8,46,37,39,13,9]; // backspace, delete, arrowleft & right, enter, tab
var isException=false;
var isDot=(190==event.keyCode); // dot
//Value which is typed
var k=String.fromCharCode(event.keyCode);

for (i=0;i<exceptions.length;i++)
{
if(exceptions[i]==event.keyCode)
isException=true;
}

if(isNaN(k) && (!isException) && (!isDot))
{
event.returnValue=false;
}
else
{
var p=new String(document.getElementById(txtidval).value+k).indexOf(“.”);

if((p<document.getElementById(txtidval).value.length-2 || isDot) && p>-1 && (!isException))
{
event.returnValue=false;
}
else if(document.getElementById(txtidval).value.length>=3 && (!isException) && p==-1)
{
//dot checking
if(event.keyCode != “190”)
{
alert(document.getElementById(txtidval).value);
event.returnValue=false;
}
}
else if(document.getElementById(txtidval).value>=3)
{
alert(document.getElementById(txtidval).value);
}
}

}
</script>
<body>
<input type=”textbox” id=”txtid” onkeydown=”doCheck(‘txtid’)”/>
</body>
</html>

i dont need output like .21 and also after entering 575 when moving curson in front of 575 it allows dot(“.”) so please kindly correct it

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@wbportJan 08.2013 — /(d*.dd)D*/ Should do it. You can take result[1] to parseFloat to read it.
Copy linkTweet thisAlerts:
@ksmarirajauthorJan 09.2013 — Hi Wbport Thanks for answering ..where we use that in my code ?
Copy linkTweet thisAlerts:
@wbportJan 09.2013 — [CODE]result = valueFromATextArea.match(/(d*.dd)D*)
if (result == null)
alert("Insert appropriate squawk here");
else
var theNumberIWantToLookAt = parseFloat(result[1]);[/CODE]
Copy linkTweet thisAlerts:
@ksmarirajauthorJan 09.2013 — Thanks wbport ?
Copy linkTweet thisAlerts:
@JMRKERJan 09.2013 — An alternative solution...
<i>
</i>&lt;!DOCTYPE HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;input type="text" onkeyup="return processNumber(this)" value=""&gt;
&lt;script type="text/javascript"&gt;
function processNumber(info) {
var v = info.value;
var n = v.split('.');
if (n[1] != undefined) { v = n[0]+'.'+n[1].substring(0,2); }
info.value = v;
return true;
}
&lt;/script&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@wbportJan 09.2013 — My first line should have read: result = valueFromATextArea.match(/(d*.dd)D*[COLOR="#FF0000"]/[/COLOR])

The last slash was needed to terminate the regular expression.
×

Success!

Help @ksmariraj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...