954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

decimal (10,2) limit

How to limit the user to decimal (10,2) when he enters number in the textfield?

Can anyone show how to do it in javascript?

trient
Newbie Poster
13 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

Please specify a bit more.
Do you already have a sort of validation function?

HenryGR
Newbie Poster
24 posts since Mar 2008
Reputation Points: 10
Solved Threads: 4
 

no i dont have validation function
how to restrict so that the currency can be limit to (10,2) decimal

trient
Newbie Poster
13 posts since Apr 2008
Reputation Points: 10
Solved Threads: 0
 

From your message, I conclude that your problem is that Javascript does not understand that for you, "10,2" equals "10.2".
Here I wrote a sample html, but the important part is the javascript function that checks the value.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>No title</title>
<script type="text/javascript" language="javascript">
	function curcheck(curf,maxval) {
		
		textcur = curf.value.toString();
		
		textcur += '';
		x = textcur.split(',');
		textcur = x[0]+'.'+x[1];
		if(Number(textcur) > maxval) {
			say('result',false); 
			return 
		}
		say('result',true);
	}

function say(were,val) {

	text = ( val ) ? "Valid value entered" : "Invalid value entered" ;
	eval("document.getElementById('"+were+"').innerHTML = text");
	
}
</script>
</head>

<body>
<form id="myform" action="" >
	<input type="text" size="4" id="currency" onchange="curcheck(this,10.2);" />
	<p id="result">&nbsp;</p>
</form>
</body>
</html>


Hope this is what you need.

HenryGR
Newbie Poster
24 posts since Mar 2008
Reputation Points: 10
Solved Threads: 4
 

You would test the form field with a regex like this.
(10, 2) in database means 10 numbers, 2 decimal places. so 12345678.90 is valid.

var testRegEx = /[0-9]{1,8}\.?[0-9]{0,2}/
var isValidNumber = form.formField.value.match(testRegEx);
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You