phone number formatting in input
I am currently creating a contact form in which there is an input for a phone number.
As I'm French, I want the phone number to match French phone number format: "01 23 45 67 89" (10 digits long written in groups of two separated by spaces).
I want to create a script that applies the changes while the user is typing.
I created this function which adds the spaces and only allow numbers, but works only if the cursor is at the end of the string (doesn't allow to insert digit elsewhere). I also noticed that there is a weird behavior if you delete all the digits and type again a phone number: it displays "0 12 34 56 78 ".
I thought the way for the function to do exactly what I want is to insert the digit, then delete all the spaces and finally add spaces at the right place, but actually I am not good at all in algorithmics :/ and I noticed that the function I created is executed before the digit is inserted so it won't work.