JavaScript - decimal validation

Asked By murrey dotnet on 31-Jul-06 06:10 AM
hi, thank you, ur code is working fine,but my requirement is to display number and after that number ,only 2 digits r allowed 
ie 100.20,11.34,45345.40,5465.76 like this, 
ur code wode working as .20,.34,.40,.76 
plz five me the reply 
thank u

code to help u out

Asked By J S on 31-Jul-06 07:17 AM
This is one of numerous solution ...[CODE]<html>
<head>
<script language=javaScript>
function checkField(fieldName,fieldValue) {
  if (fieldValue.length>0           //field is not empty
     &&
     !isNaN(fieldValue)              //field contains a number
     &&
//  typos are killing me
     parseFloat(fieldValue) * 100 != parseInt(parseFloat(fieldValue)*100))
{    //has more than 2 dec's
       alert ('use no more than 2 decimals please');
       fieldName.select();
       fieldName.focus();
  }
}
</script>
</head>
<body>
<form>
<input type='text' name='aField' onBlur='checkField(this, this.value)'>
</form>
</body>
</html> [/CODE]

Regexp validation for num with 2 decimal places

Asked By mv ark on 31-Jul-06 09:20 AM
Try this function-
function IsMatch(str)
{
	mystring = str;
	if (mystring.match(/^\d+\.\d{2}$/ ) ) {
		alert("match");
	}
	else
	{
		alert("not a match");
	}
}
\d+ indicates one or more numbers.
\d{2} indicates exactly 2 numbers are required.
0.20  is allowed but not .20