Issue
I have a form where chilean customers need to write their security number ( RUT )
the format can be with 1.234.567-8 for old people or 12.345.678-9 for younger people
What I need is that when they type down their number the input text change as soon as they type down and start formatting the number with the two dots and - like in the example above
I created this code
<input type="text" class="inputs" name="cliente" id="cliente"
onkeydown = "this.value = this.value.replace( /^(\d{1})(\d{3})(\d{3})(\w{1})$/, '$1.$2.$3-$4')" >
It works almost good but only with 9 digits RUTs but not with 8 digits RUTs, any idea how to accomplish this ?
Solution
This logic removes any - or . you previously put in it, or the user put in it, and then performs the reformatting. It also fires on keyup rather than keydown as the new value would not have been added yet on the keydown.
function formatCliente (cliente) {
cliente.value = cliente.value
.replace(/[^0-9]/g, '')
.replace( /^(\d{1,2})(\d{3})(\d{3})(\w{1})$/, '$1.$2.$3-$4')
}
<input type="text" class="inputs" name="cliente"
id="cliente" onkeyup="formatCliente(this)">
Answered By - Taplar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.