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?

Recommended Answers

All 4 Replies

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

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

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.

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.