In this Pen i'll show you how to make input form for polish postal code with simple assist and validation.
A Pen by Paweł Mansfeld on CodePen.
<form> | |
<input type="text" required class="postalcode" placeholder="Kod pocztowy:" pattern="[0-9]{2}[-][0-9]{3}"> | |
<input type="submit" value="Wyślij"> | |
</form> |
In this Pen i'll show you how to make input form for polish postal code with simple assist and validation.
A Pen by Paweł Mansfeld on CodePen.
$(document).on('keyup', '.postalcode', function(e){ | |
var code = $(this).val(); | |
var key = event.keyCode || event.charCode; | |
if($(this).val().length == 2){ | |
if( key == 8 || key == 46 ){ | |
} | |
else{ | |
$(this).val(code+'-'); | |
} | |
} | |
if($(this).val().indexOf('--') !== -1){ | |
$(this).val(code.replace('--','-')); | |
} | |
}); |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> |
input{ | |
padding:8px; | |
border:1px solid #aaa; | |
border-radius:4px; | |
font-size:18px; | |
} |