luximetr/AnyFormatKit

Prevention from passing non-numeric signs

Closed this issue · 2 comments

It would be great if the text field would remove non-numeric signs on phone paste (f.e. pasted tel. +48-123-123-123 to (48)-123-123-123 for pattern (##)-###-###-###) - so text field receives only numbers from pasted text and formats it accordingly.

you always can use onReceive property to prevent typing anything but numbers
something like this

FormatTextField(
    unformattedText: $phone,
    placeholder: "900 000 00 00",
    textPattern: "### ### ## ##"
)
    .keyboardType(.numberPad)
    .onReceive(Just(phone)) { newValue in
        let filtered = newValue.filter { "0123456789".contains($0) }
        if filtered != newValue {
            self.phone = filtered
        }
    }

@swiatrzyk Thank you for your idea. Will keep it in mind when will work on it in future.