AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



html-jquery: Why maxlength is not working for number type input field?

I am using input type="number" to input only numeric values. But it is not restricting the length of input values maxlength works for simple text field. why it´s not working for type="number". Give me some alternative solution, i want to restrict the length of a number while entering the value.

html x 24
jquery x 12
Posted On : 2015-11-30 23:40:57.0
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250041
up-rate
4
down-rate

Answers


maxlength has been deprecated for type="number". Instead of that you can use max and size attribute for type="number". size holds the length of the field and max holds the possible maximum value for that field. If you want to restrict the length while entering the value you can disable it by jquery or javascript. Let's see the following example:

MaxLength.html:
------------------------------------------------
<html>
<head><title>Maxlength for type="number"</title></head>
<body>
Employee Name: <input type="text" name="name" maxlength="15"/>
Employee ID: <input type="number" name="id" id="emp" size="10" max="9999999999"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$('#emp').bind('keypress', function(){
if($(this).val().length>=$(this).attr('size')){
return false;
}
});
</script>
</body>
</html>

Output:
-----------------------
Employee Name: Employee ID:

Posted On : 2015-12-01 00:18:29
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in