Click here to Skip to main content
15,846,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
javascript that accepts only percentage values
Posted
Comments
CRDave1988 8-Feb-12 1:50am    
can u be more clear?

XML
use a Range Validator for validating values in textbox
as percentage.set the MinimumValue as 0 and MaximumValue as 500.

For example you have texbox
<asp:Texbox ID="txtPercentage" runat=server></Text>

just set this ControlToValidate="txtPercentage"

<asp:RangeValidator ID="ranValidator" runat="server"             ErrorMessage="RangeValidator" ControlToValidate="txtPercentage"             MaximumValue="500" MinimumValue="0" Type="Double"></asp:RangeValidator>
 
Share this answer
 
v2
Comments
kirrri 8-Feb-12 2:14am    
i dont want validation control to use so plz reply javascript
CRDave1988 8-Feb-12 3:22am    
5
Hi,

You can try this one.

JavaScript
function Validate() {
    var o = document.getElementById("valueCent");
    var val = o.value;
    if (val.length == 0) {
        alert("Please Enter Value");
        return;
    }
    var index = val.search(/[^0-9\.]/);
    if (index != -1) {
        o.selectionStart = o.selectionEnd = index;
        alert("Invalid Characters");
        return;
    }
    if (val.match(/\./g).length > 1)
    {
        alert("Invalid Number");
        return;
    }
    var floatVal = parseFloat(val);
    if (floatVal < 0 || floatVal > 100)
    {
        alert("Value must be between 0.00 and 100.00");
        return;
    }
    alert("Valid value of: " + floatVal.toFixed(2));
}

And to your HTML
HTML
<input id="valueCent" type="text" size="20">
<input type="button" value="Check"  önclick="Validate()">
 
Share this answer
 
v2
Comments
Anuja Pawar Indore 8-Feb-12 3:17am    
Added pre tag
CRDave1988 8-Feb-12 3:21am    
5

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