Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
validation for textbox using javascript which takes only values like 12.12 before and after dot only 2 values
Posted
Updated 17-Feb-12 6:28am
v2
Comments
BobJanova 17-Feb-12 12:28pm    
Changed your tags, this is not a C# question.

Use maxlenth as 5. Handle on keypress event to allow only numbers n dots n regex for format.
 
Share this answer
 
Hey
C#
function onlyDouble(evt) {
            var val1;
            if (!(evt.keyCode == 46 || (evt.keyCode >= 48 && evt.keyCode <= 57)))
                return false;
            var parts = evt.srcElement.value.split('.');
            if (parts.length > 2)
                return false;
            if (evt.keyCode == 46)
                return (parts.length == 1);
            if (evt.keyCode != 46) {
                var currVal = String.fromCharCode(evt.keyCode);
                val1 = parseFloat(String(parts[0]) + String(currVal));
                if(parts.length==2)
                    val1 = parseFloat(String(parts[0])+ "." + String(currVal));
            }

            if (val1 > 99.99)
                return false;
            if (parts.length == 2 && parts[1].length >= 2) return false;
        }


//calling on page load
txtPer.Attributes["onkeypress"]="return onlyDouble(event)";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900