Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mansfeldpl/0dda4a0f797d97b02e2426975a067fb3 to your computer and use it in GitHub Desktop.
Save mansfeldpl/0dda4a0f797d97b02e2426975a067fb3 to your computer and use it in GitHub Desktop.
Polish postal code - input assist and validation
<form>
<input type="text" required class="postalcode" placeholder="Kod pocztowy:" pattern="[0-9]{2}[-][0-9]{3}">
<input type="submit" value="Wyślij">
</form>

Polish postal code - input assist and validation

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.

License.

$(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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment