I have a credit card number input and I am trying to add an image of the credit card brand next to the text field based on the number the user entered. whether that may be visa, master card, etc.
I have created a struct for the credit cards:
struct CreditCard: Identifiable, Codable {
var id: String = UUID().uuidString
var type: String
var prefix: [Int]
var length: Int
}
and I have added the credit card data locally:
let cardTypes: [CreditCard] = [
CreditCard(type: "American Express", prefix: [34, 37], length: 15),
CreditCard(type: "Visa", prefix: [4], length: 16),
CreditCard(type: "Master Card", prefix: [51, 52, 53, 54], length: 16),
CreditCard(type: "Discover", prefix: [6011], length: 16),
CreditCard(type: "JCB", prefix: [3, 2131, 1800], length: 15)
]
I have imported the data into my view using the following:
var creditCards: [CreditCard] = cardTypes
So for instance, I'd like to display an image saved in my assets folder with the name "Master Card" only when the first numbers in the text input are 51, 52, 53 or 54.
I am using a swiftui textfield
Any help is appreciated, thank you!
