Hello guys, I want to validated in my textbox only numbers shoud be entered and i found this code.

function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;


then i want to validated again only 1 number should be allowed . how to do this? tnx in advance. I mean. When you entered in textbox, only 1 be appeared and then the highest value to entred is 5 greater than five is not allowed. only 1-5. tnx guys.

Recommended Answers

All 3 Replies

You don't have to rely on JavaScript for this anymore. Use the HTML 5 <!DOCTYPE html> and you can use:

<input type="number" min="1" max="5" required>

Sorry. I recently used type="range" and I checked caniuse to make sure most current browsers supported it. I didn't notice type="number" wasn't supported by Firefox (which seems odd, but...). As for IE, I figure IE9 and IE10 will disappear quickly, and I've just about given up on IE6-8. I considered mentioning that the target of the form should always validate anyway, along with some cautions about HTML5 support, but I was trying to be brief.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.