Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mask input with percent sign

I would like to allow the user to enter 1 or two numerical digits, and display the digits followed by a percent sign. If no digits are entered, it should be blank. The attached almost works, but doesn't show the percent sign if only one digit is entered. Can anyone help? Thanks

http://jsfiddle.net/dm8Mb/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" /> 
<title>Mask</title> 

<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script src="http://cloud.github.com/downloads/digitalBush/jquery.maskedinput/jquery.maskedinput-1.3.js" type="text/javascript"></script>

<script> 
$(function(){$(".percent").mask("9?9%");});
</script>

</head>

<body>
<input type="text" name="percent" class="percent" value="" />
</body> 
</html> 
like image 779
user1032531 Avatar asked Jul 23 '12 13:07

user1032531


1 Answers

Try this - http://jsfiddle.net/dm8Mb/12/

$(function(){
    $(".percent").mask("9?9%");

    $(".percent").on("blur", function() {
        var value = $(this).val().length == 1 ? $(this).val() + '%' : $(this).val();
        $(this).val( value );
    })
});
like image 108
Zoltan Toth Avatar answered Sep 17 '22 20:09

Zoltan Toth