Skip to content

Commit

Permalink
Fixing required validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sebaland committed Mar 11, 2024
1 parent 438e6bc commit 75c70d1
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Sources/Authenticator/Views/Primitives/PhoneNumberField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ struct PhoneNumberField: View {
.foregroundColor(foregroundColor)
.focused($focusedField, equals: .callingCode)
.onChange(of: callingCode) { code in
if !phoneNumber.isEmpty {
text = "\(code)\(phoneNumber)"
if !numericPhoneNumber.isEmpty {
text = "\(code)\(numericPhoneNumber)"
}
}

Expand All @@ -80,13 +80,11 @@ struct PhoneNumberField: View {
return
}

if phoneNumber.isEmpty {
if numericPhoneNumber.isEmpty {
// If the phone number is empty, we consider this to be an empty input regardless of the calling code, as that one is automatically populated
self.text = ""
} else {
// Only numbers are allowed by the service, so remove other characters in the internally tracked full phone number
let onlyNumbers = phoneNumber.filter("0123456789".contains)
self.text = "\(callingCode)\(onlyNumbers)"
self.text = "\(callingCode)\(numericPhoneNumber)"
}

if validator.state != .normal || !phoneNumber.isEmpty {
Expand Down Expand Up @@ -154,6 +152,10 @@ struct PhoneNumberField: View {
case callingCode
case phoneNumber
}

private var numericPhoneNumber: String {
return phoneNumber.filter("0123456789".contains)
}
}

/// This allows the user to select a dialing code from a list of all available ones,
Expand Down

0 comments on commit 75c70d1

Please sign in to comment.