Skip to content

How to only allow and format certain characters for TextField input. #144

Discussion options

You must be logged in to vote

The release is working great! Thanks for the help.

Just for future reference if anyone sees this in the future, here's the final version that works on both iOS & Android.

func onlyDigits(_ input: String) -> String {
  #if SKIP
    let validChars = "0123456789".toSet() // Remove once Skip supports Set
  #else
    let validChars = Set("0123456789")
  #endif
  
  let out = input.filter{ validChars.contains($0) }
  print("in: \(input) out: \(out)")
  return out
}

public var body: some View {
  #if !SKIP
  // For some reason this is a bit glitchy on Android
    TextField("Placeholder", text: $text)
      .onChange(of: text) {_ in
        text = onlyDigits(text)
      }
  #else
    // For som…

Replies: 2 comments 8 replies

Comment options

You must be logged in to vote
5 replies
@joshuakcockrell
Comment options

@joshuakcockrell
Comment options

@joshuakcockrell
Comment options

@joshuakcockrell
Comment options

@aabewhite
Comment options

Comment options

You must be logged in to vote
3 replies
@aabewhite
Comment options

@joshuakcockrell
Comment options

Answer selected by joshuakcockrell
@aabewhite
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants