<html>
<head>
<script>
function floatValidation(e,control)
{
if (e.keyCode==46)
{
var patt1=new RegExp("\\.");
var ch =patt1.exec(control.value);
if(ch==".")
{
e.keyCode=0;
}
}
else if( (e.keyCode>=48 && e.keyCode<=57) || e.keyCode==8)//Numbers or BackSpace
{
if (control.value.indexOf('.') != -1)//. Exist in TextBox
{
var pointIndex=control.value.indexOf('.');
var beforePoint = control.value.substring(0,pointIndex);
var afterPoint = control.value.substring(pointIndex+1);
var iCaretPos = 0;
if (document.selection)
{
if (control.type == 'text') // textbox
{
var selectionRange = document.selection.createRange();
selectionRange.moveStart ('character', -control.value.length);
iCaretPos = selectionRange.text.length;
}
}
if(iCaretPos > pointIndex && afterPoint.length >= 2)
{
e.keyCode=0;
alert('You enter two digit only after decimal point');
}
else if(iCaretPos <= pointIndex && beforePoint.length >= 7)
{
e.keyCode=0;
}
}
else//. Not Exisist in TextBox
{
if(control.value.length >= 7)
{
e.keyCode=0;
alert('You enter 7 digit only before decimal point');
}
}
}
else
{
e.keyCode=0;
}
}
</script>
</head>
<body>
<input id="amt" type="text" name="Amount" onkeypress="return floatValidation(event,this)" onfocus="setCaretPosition(this,0);"/>
</body>
</html>
Number validation using javascript
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment