user3103915 user3103915 - 4 months ago 11
jQuery Question

Default value in an input tag at a particular position

I don't know if this is possible or not and hence here's the part. Suppose I have an input tag like the following.

<input id="expiry" name="expiry" type="text" placeholder="MM/YY">


Now what I want is that the user should already see the '/' part, i.e when he types 1212, the textbox should become 12/12 automatically, can it be done if yes , how. Thanks in advance.

Answer
<input id="expiry" name="expiry" type="text" placeholder="MM/YY">
<script type="text/javascript">
   $("#expiry").bind('keyup mouseup', function () {
   if($('#expiry').val().length ==2){
   $('#expiry').val($('#expiry').val()+'/');
   }        
});

</script>

check the fiddle

Comments