Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
how to give validation on text box that it should accept only integer and range between 5-100


i am able to validate the input accept no only using key press but not give range

What I have tried:

i am able to validate the input accept no only using key press but not give range
onkeypress="return isNumberKey(event)"



<script language="Javascript">

function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : evt.keyCode
if (charCode = 46 && charCode > 31
&& (charCode < 48 || charCode > 57)) {
alert("Input should be numeric only");
return false;


}

return true;
}

</script>
Posted
Comments
Kornfeld Eliyahu Peter 17-Feb-16 7:10am    
Search for the JQuery Validation extension...

1 solution

Your current isNumberKey function will not work as you expect it to. It currently only rejects character 46 (Delete), which is one of the characters you should be allowing.
JavaScript
function isNumberKey(event){
    var charCode = event.which || event.keyCode;
    switch (charCode) {
        case 8: // Backspace
        case 35: // End
        case 36: // Home
        case 37: // Left arrow
        case 38: // Up arrow
        case 39: // Right arrow
        case 40: // Down arrow
        case 46: { // Delete
            return true;
        }
        default: {
            if (48 <= charCode && charCode <= 57) {
                // 0 - 9
                return true;
            }
            
            return false;
        }
    }
}

Javascript key codes | CSS-Tricks[^]

You should be using the built-in support in modern browsers:
HTML Input Types[^]
HTML
<input type="number" min="5" max="100" ... />

That has fairly decent browser support:
Can I use... Number input type[^]

Then, as mentioned in the comments, use the jQuery Validation Plugin[^] to validate your input.
 
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