<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Suraj Thomas K on Medium]]></title>
        <description><![CDATA[Stories by Suraj Thomas K on Medium]]></description>
        <link>https://medium.com/@surajthomask?source=rss-5c254926ad5b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*SHk8y4Wmhok5sTrrOJFZKw.jpeg</url>
            <title>Stories by Suraj Thomas K on Medium</title>
            <link>https://medium.com/@surajthomask?source=rss-5c254926ad5b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 00:56:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@surajthomask/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introducing ATGValidator for Swift]]></title>
            <link>https://medium.com/ounass/introducing-atgvalidator-for-swift-628f48b61ee5?source=rss-5c254926ad5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/628f48b61ee5</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[forms]]></category>
            <category><![CDATA[validation]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Suraj Thomas K]]></dc:creator>
            <pubDate>Wed, 11 Mar 2020 13:10:07 GMT</pubDate>
            <atom:updated>2020-03-11T13:10:07.220Z</atom:updated>
            <content:encoded><![CDATA[<h4>A simple, easy to use, open-source validation framework written in Swift</h4><p>When we started working on our first in-house eCommerce apps at Al Tayer Digital, one of the challenges we faced was performing user input validations efficiently and in a maintainable way. Our initial solution for validation was by adding ad-hoc solutions including length/character-set checks and regex string matching. Needless to say, in a few weeks, it became too difficult to track these pieces of code distributed across the project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Duer1F9sk_ET4ECu_TTPUw.jpeg" /></figure><p>We started exploring open source frameworks to find if there is any that will match our requirements. Our main focus was on ease of use, low boilerplate code, and most importantly form validation. The main contenders for selection were <a href="https://github.com/SwiftValidatorCommunity/SwiftValidator">SwiftValidator</a> and <a href="https://github.com/adamwaite/Validator">Validator</a>.</p><p>Both frameworks were rule-based but were functioning in different ways. SwiftValidator performed validations based on a Validator instance, and apps need to manually trigger validation by hooking up with UITextField and UITextView delegates. This introduced a lot of boilerplate code, and we wanted to avoid this.</p><p>Validator framework addressed this issue by adding targets to UITextField and UITextView in framework, and running validation when specific events occurred. This was looking promising, except one aspect. Validator was using generics extensively, and this made it impossible to club together different validation results. So no form validation was possible with it.</p><p>So we wanted to combine the best aspects of both frameworks and create a new framework that is easy to use, with minimal boiler-plate code, and form validation support. And that’s when we wrote <a href="https://github.com/altayer-digital/ATGValidator">ATGValidator</a>.</p><blockquote><a href="https://github.com/altayer-digital/ATGValidator">ATGValidator</a> is rule based validation framework written in a protocol based approach. You get a set of predefined rules, and can define new rules easily. UITextField and UITextView has build in support for validation, and it’s easy to add validation support for custom UI elements. You get a form validator which can group multiple UI elements together and give a unified result for validation. And you can run validations on individual data types like String, Int, Double etc independently without need for a UI element.</blockquote><p>Let me show how easy it is to use the framework with an example below. Download the starter project from <a href="https://github.com/altayer-digital/examples/raw/master/iOS/FormValidationSample_StarterProject.zip">here</a>.</p><p>Unzip the downloaded file, and go to the folder in Terminal, and run</p><pre><em>pod install</em></pre><p>Now open the generated xcworkspace file, and run the app. You can see a basic signup form implemented in the app. Now, let’s add some validation.</p><p>Import <a href="https://github.com/altayer-digital/ATGValidator">ATGValidator</a> in ViewController.swift</p><pre>import ATGValidator</pre><p>Create an instance of FormValidator</p><pre><strong>let</strong> formValidator = FormValidator()</pre><p>Now in viewDidLoad, add the following lines of code</p><pre>nameTextField.validationRules = [StringLengthRule.min(5)]<br>numberTextField.validationRules = [<br>    StringLengthRule(min: 8, max: 12, ignoreCharactersIn: CharacterSet.whitespacesAndNewlines),<br>    CharacterSetRule.numbersOnly(ignoreCharactersIn: CharacterSet.whitespacesAndNewlines)<br>]<br>addressTextView.validationRules = [StringLengthRule.min(20)]<br>passwordTextField.validationRules = [<br>    StringLengthRule.min(6),<br>    CharacterSetRule.containsNumber(),<br>    CharacterSetRule.containsUpperCase(),<br>    CharacterSetRule.containsLowerCase()<br>]<br>confirmPasswordTextField.validationRules = [<br>    StringValueMatchRule(base: passwordTextField),<br>    StringLengthRule.required()<br>]</pre><pre>formValidator.add(nameTextField)<br>formValidator.add(numberTextField)<br>formValidator.add(addressTextView)<br>formValidator.add(passwordTextField)<br>formValidator.add(confirmPasswordTextField)</pre><p>Now add a validation handler inside `didTapOnProceedButton` method as shown below;</p><pre>formValidator.validateForm(completion: { result <strong>in<br>    let</strong> alert = UIAlertController(<br>        title: &quot;Validation result&quot;,<br>        message: result.status == .success ? &quot;Successful&quot; : &quot;Failed&quot;,<br>        preferredStyle: .alert<br>    )<br>    alert.addAction(UIAlertAction(title: &quot;OK&quot;, style: .cancel))<br>    <strong>self</strong>.present(alert, animated: <strong>true</strong>)<br>})</pre><p>Now when you tap on the proceed button, the framework validates the input with rules and provides a result object with an array of errors and the result is shown in an alert view.</p><p>Just showing if validation failed or succeeded will not be good enough to provide a good user experience. So, let’s add some meaningful error messages to the rules.</p><pre>nameTextField.validationRules = [<br>  StringLengthRule.min(5).with(errorMessage: &quot;Name needs to have atleast 5 characters&quot;)<br>]<br>numberTextField.validationRules = [<br>  StringLengthRule(<br>    min: 8,<br>    max: 12,<br>    ignoreCharactersIn: CharacterSet.whitespacesAndNewlines<br>  ).with(errorMessage: &quot;Phone number must be between 8 and 12 digits long&quot;),<br>  CharacterSetRule.numbersOnly(<br>    ignoreCharactersIn: CharacterSet.whitespacesAndNewlines<br>  ).with(errorMessage: &quot;Phone number must have only numbers&quot;)<br>]<br>addressTextView.validationRules = [<br>  StringLengthRule.min(20).with(errorMessage: &quot;Address must have atleast 20 characters&quot;)<br>]<br>passwordTextField.validationRules = [<br>  StringLengthRule.min(6).with(errorMessage: &quot;Password must have atleast 6 characters&quot;),<br>  CharacterSetRule.containsNumber().with(errorMessage: &quot;Password must have atleast 1 number&quot;),<br>  CharacterSetRule.containsUpperCase().with(errorMessage: &quot;Password must have atleast 1 uppercase&quot;),<br>  CharacterSetRule.containsLowerCase().with(errorMessage: &quot;Password must have atleast 1 lowercase&quot;)<br>]<br>confirmPasswordTextField.validationRules = [<br>  StringValueMatchRule(base: passwordTextField).with(errorMessage: &quot;Passwords does not match&quot;),<br>  StringLengthRule.required().with(errorMessage: &quot;Confirm password field cannot be empty&quot;)<br>]</pre><blockquote><em>ValidationError is a framework level enumeration conforming to Error protocol and is used for identifying default errors. It has an additional custom case with error message to be used for easy error handling.</em></blockquote><blockquote><em>Rule protocol has 2 helper methods to associate an error object or an error message to itself. The methods being used in above code sample is the one with errorMessage.</em></blockquote><p>And update the form validation handler as follow;</p><pre>formValidator.validateForm(completion: { result <strong>in<br>  let</strong> message = result.errors?.reduce(&quot;&quot;, { (string, error) -&gt; String <strong>in<br>    let</strong> validationError = error <strong>as</strong>? ValidationError<br>    <strong>return</strong> string + (validationError?.customErrorMessage ?? String(describing: validationError)) + &quot;\n&quot;<br>  })</pre><pre><strong>  let</strong> alert = UIAlertController(<br>    title: &quot;Validation &quot; + (result.status == .success ? &quot;Successful&quot; : &quot;Failed&quot;),<br>    message: message,<br>    preferredStyle: .alert<br>  )<br>  alert.addAction(UIAlertAction(title: &quot;OK&quot;, style: .cancel))<br>  <strong>self</strong>.present(alert, animated: <strong>true</strong>)<br>})</pre><p>The snippet above goes through all the error objects passed in from the validation result and creates an alert message from it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HC5rZXhOdEr_y0EDzdutdQ.jpeg" /></figure><p>This will not be the best experience we can give to the user. Rather than showing it in an alert view, let’s update the experience to give more context to the specified errors.</p><p>Let’s move around and clean up the code a bit to make it more readable.</p><p>First, create a <em>Constants</em> enumeration inside the <em>Viewcontroller </em>class.</p><pre><strong>private</strong> <strong>enum</strong> Constants {<br>  <strong>static</strong> <strong>let</strong> cornerRadius: CGFloat = 3.0<br>  <strong>static</strong> <strong>let</strong> borderWidth: CGFloat = 1.0<br>  <strong>static</strong> <strong>let</strong> errorColor: CGColor = UIColor.red.cgColor<br>  <strong>static</strong> <strong>let</strong> validColor: CGColor = UIColor.clear.cgColor<br>}</pre><p>Now move all validation rule setting to a new method.</p><pre><strong>private</strong> <strong>func</strong> configureValidations() {<br>    nameTextField.validationRules = [StringLengthRule.min(5).with(errorMessage: &quot;Name needs to have atleast 5 characters&quot;)]<br>    numberTextField.validationRules = [<br>        StringLengthRule(min: 8, max: 12, ignoreCharactersIn: CharacterSet.whitespacesAndNewlines).with(errorMessage: &quot;Phone number must be between 8 and 12 digits long&quot;),<br>        CharacterSetRule.numbersOnly(ignoreCharactersIn: CharacterSet.whitespacesAndNewlines).with(errorMessage: &quot;Phone number must have only numbers&quot;)<br>    ]<br>    addressTextView.validationRules = [StringLengthRule.min(20).with(errorMessage: &quot;Address must have atleast 20 characters&quot;)]<br>    passwordTextField.validationRules = [<br>        StringLengthRule.min(6).with(errorMessage: &quot;Password must have atleast 6 characters&quot;),<br>        CharacterSetRule.containsNumber().with(errorMessage: &quot;Password must have atleast 1 number&quot;),<br>        CharacterSetRule.containsUpperCase().with(errorMessage: &quot;Password must have atleast 1 uppercase&quot;),<br>        CharacterSetRule.containsLowerCase().with(errorMessage: &quot;Password must have atleast 1 lowercase&quot;)<br>    ]<br>    confirmPasswordTextField.validationRules = [<br>        StringValueMatchRule(base: passwordTextField).with(errorMessage: &quot;Passwords does not match&quot;),<br>        StringLengthRule.required().with(errorMessage: &quot;Confirm password field cannot be empty&quot;)<br>    ]</pre><pre>    formValidator.add(nameTextField)<br>    formValidator.add(numberTextField)<br>    formValidator.add(addressTextView)<br>    formValidator.add(passwordTextField)<br>    formValidator.add(confirmPasswordTextField)<br>}</pre><p>Create a new method to register validation handlers for individual fields and update the UI to display errors.</p><pre><strong>private</strong> <strong>func</strong> configureFieldErrorHandlers() {<br>    nameTextField.validationHandler = { [<strong>weak</strong> <strong>self</strong>] result <strong>in<br>        self</strong>?.nameTextField.layer.borderColor = result.status == .failure ? Constants.errorColor : Constants.validColor<br>        <strong>self</strong>?.nameTextField.layer.borderWidth = result.status == .failure ? Constants.borderWidth : 0.0<br>        <strong>self</strong>?.nameErrorLabel.text = (result.errors?.first <strong>as</strong>? ValidationError)?.customErrorMessage<br>    }</pre><pre>    numberTextField.validationHandler = { [<strong>weak</strong> <strong>self</strong>] result <strong>in<br>        self</strong>?.numberTextField.layer.borderColor = result.status == .failure ? Constants.errorColor : Constants.validColor<br>        <strong>self</strong>?.numberTextField.layer.borderWidth = result.status == .failure ? Constants.borderWidth : 0.0<br>        <strong>self</strong>?.numberErrorLabel.text = (result.errors?.first <strong>as</strong>? ValidationError)?.customErrorMessage<br>    }</pre><pre>    addressTextView.validationHandler = { [<strong>weak</strong> <strong>self</strong>] result <strong>in<br>        self</strong>?.addressTextView.layer.borderColor = result.status == .failure ? Constants.errorColor : Constants.validColor<br>        <strong>self</strong>?.addressTextView.layer.borderWidth = result.status == .failure ? Constants.borderWidth : 0.0<br>        <strong>self</strong>?.addressErrorLabel.text = (result.errors?.first <strong>as</strong>? ValidationError)?.customErrorMessage<br>    }</pre><pre>    passwordTextField.validationHandler = { [<strong>weak</strong> <strong>self</strong>] result <strong>in<br>        self</strong>?.passwordTextField.layer.borderColor = result.status == .failure ? Constants.errorColor : Constants.validColor<br>        <strong>self</strong>?.passwordTextField.layer.borderWidth = result.status == .failure ? Constants.borderWidth : 0.0<br>        <strong>self</strong>?.passwordErrorLabel.text = (result.errors?.first <strong>as</strong>? ValidationError)?.customErrorMessage<br>    }</pre><pre>    confirmPasswordTextField.validationHandler = { [<strong>weak</strong> <strong>self</strong>] result <strong>in<br>        self</strong>?.confirmPasswordTextField.layer.borderColor = result.status == .failure ? Constants.errorColor : Constants.validColor<br>        <strong>self</strong>?.confirmPasswordTextField.layer.borderWidth = result.status == .failure ? Constants.borderWidth : 0.0<br>        <strong>self</strong>?.confirmPasswordErrorLabel.text = (result.errors?.first <strong>as</strong>? ValidationError)?.customErrorMessage<br>    }<br>}</pre><blockquote>Above method has a lot of repeating code for displaying error messages, but I won’t be covering it’s refactor in this article.</blockquote><p>Since we will be showing validation errors on individual fields, we don’t need to show the alert on tapping the proceed button. So, let’s update the <em>FormValidator </em>handler closure to update the state of the proceed button based on validation result. Add a new method as shown below;</p><pre><strong>private</strong> <strong>func</strong> configureFormHandler() {<br>    proceedButton.isEnabled = <strong>false<br>    </strong>proceedButton.alpha = 0.5<br>    formValidator.handler = { [<strong>weak</strong> <strong>self</strong>] result <strong>in<br>        let</strong> isFormValid = result.status == .success<br>        <strong>self</strong>?.proceedButton.isEnabled = isFormValid<br>        <strong>self</strong>?.proceedButton.alpha = isFormValid ? 1.0 : 0.5<br>    }<br>}</pre><p>And remove the unnecessary code in IBAction for the proceed button as shown below;</p><pre><strong>@IBAction</strong> <strong>private</strong> <strong>func</strong> didTapOnProceedButton() {<br>    // TODO: Perform proceed action.<br>}</pre><p>Now, let’s update the viewDidLoad method to call these methods;</p><pre><strong>override</strong> <strong>func</strong> viewDidLoad() {<br>    <strong>super</strong>.viewDidLoad()</pre><pre>    addObserversForKeyboard()<br>    <br>    nameTextField.layer.cornerRadius = Constants.cornerRadius<br>    numberTextField.layer.cornerRadius = Constants.cornerRadius<br>    addressTextView.layer.cornerRadius = Constants.cornerRadius<br>    passwordTextField.layer.cornerRadius = Constants.cornerRadius<br>    confirmPasswordTextField.layer.cornerRadius = Constants.cornerRadius</pre><pre>    configureFormHandler()<br>    configureValidations()<br>    configureFieldErrorHandlers()<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/352/1*Z3gmRK_U11tX2DouzCqLNw.gif" /></figure><p>Build and run the app. Voila, now we have a fully functional form validation in place. See it in action below;</p><p>You can download the complete sample project from <a href="https://github.com/altayer-digital/examples/raw/master/iOS/FormValidationSample_CompleteProject.zip">here</a>.</p><p>There are many more things under the hood in <a href="https://github.com/altayer-digital/ATGValidator">ATGValidator</a>. And covering them all will not be possible in a single article.</p><p>If you want to learn in-depth about the framework head over to the readme section of the project in <a href="https://github.com/altayer-digital/ATGValidator">Github</a>.</p><p>To learn more about using ATGValidator to validate credit card numbers, head over to this <a href="https://medium.com/altayer-digital/credit-debit-card-validation-and-type-detection-in-swift-using-atgvalidator-10514246c634">article</a>.</p><p>If you found this article helpful, please don’t forget to clap. Cheers..!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=628f48b61ee5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ounass/introducing-atgvalidator-for-swift-628f48b61ee5">Introducing ATGValidator for Swift</a> was originally published in <a href="https://medium.com/ounass">Ounass</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Credit/Debit card validation and type detection in Swift using ATGValidator]]></title>
            <link>https://medium.com/ounass/credit-debit-card-validation-and-type-detection-in-swift-using-atgvalidator-10514246c634?source=rss-5c254926ad5b------2</link>
            <guid isPermaLink="false">https://medium.com/p/10514246c634</guid>
            <category><![CDATA[validation]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[credit-cards]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Suraj Thomas K]]></dc:creator>
            <pubDate>Mon, 09 Mar 2020 07:57:32 GMT</pubDate>
            <atom:updated>2020-03-09T18:17:06.116Z</atom:updated>
            <content:encoded><![CDATA[<p>Payments using credit and debit cards are an integral part of eCommerce journey. If you have an app that sells any sort of merchandise that’s not app content and not related to iOS ecosystem, you would have already exposed a way to make payments using credit/debit cards.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_fi-xsfqDgTQCivURGd3Ow.jpeg" /></figure><p>It’s pretty common for users to enter credit card numbers incorrectly, and showing an error message to the user after an error from payment gateway is something unintuitive. We can change this flow by pointing out to the user if the entered card number is invalid while the user is entering it. And even better we can detect the type of card while the number is being entered. Please note that we will be doing this locally and no communication is needed with any servers for this. Let’s see how we can give a reassuring experience to the user while they are entering credit/debit card number.</p><p>We can use <a href="https://github.com/altayer-digital/ATGValidator">ATGValidator</a> for doing this few lines of code. ATGValidator can be added using both Cocoapods and Carthage. We will be using Cocoapods in this article.</p><blockquote><a href="https://github.com/altayer-digital/ATGValidator">ATGValidator</a> is a validation framework written completely in Swift, and has validation rules as it’s backbone. It provides a lot of prebuilt validation rules and supports adding your own validation rules. ATGValidator supports a form validator which can be used to club together multiple input fields and use their validation results as a single unit.</blockquote><p>Prerequisites:- Xcode, Cocoapods (If you are not familiar with Cocoapods, have a look at this <a href="https://guides.cocoapods.org/using/getting-started.html">link</a> first.)</p><p>Starter project - You can download the starter project from <a href="https://github.com/altayer-digital/examples/raw/master/iOS/CardValidationSample_StarterProject.zip">here</a>.</p><p>Once the starter project is downloaded, unzip and open it in Xcode.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/754/1*TirKtJXIVK63HesY4sF21A.png" /></figure><p>When you compile and run the downloaded project, you can see a text field for credit/debit card number entry.</p><p>There is no validation added yet, and when you tap on the continue button it just prints out the value entered in the text field.</p><p>Let’s add some validation to the fields.</p><p>The first thing to do is to add ATGValidator as a dependency in Podfile. For that, open Podfile in a text editor and uncomment the line (remove the <strong># </strong>from the line) where ATGValidator is mentioned.</p><pre>pod ‘ATGValidator’, ‘~&gt; 1.0’</pre><p>Now open the project directory in Terminal, and run</p><pre>pod install</pre><p>This will download and link the framework with the project and generate an xcworkspace file. Double click on the generated CardValidationSample.xcworkspace file to open it in Xcode.</p><p>Add the following line to <em>ViewController.swift</em> below UIKit import</p><pre><strong>import</strong> ATGValidator</pre><p>Now we can set up the credit card field for validations. Add the following lines of code to the end of viewDidLoad method.</p><pre>// Line 1<br>cardNumberTextField.validationRules = [<br>    PaymentCardRule(acceptedTypes: [.visa])<br>]<br>// Line 2<br>cardNumberTextField.validateOnInputChange(<strong>true</strong>)<br>// Line 3<br>cardNumberTextField.validationHandler = { result <strong>in<br>    if</strong> <strong>self</strong>?.cardNumberTextField.text?.isEmpty ?? <strong>true</strong> {<br>        <strong>self</strong>?.cardTypeIndicator.text = <strong>nil<br>    </strong>} <strong>else</strong> {<br>        <strong>self</strong>?.cardTypeIndicator.text = (result.status == .success ? &quot;✅&quot; : &quot;❌&quot;)<br>    }<br>    <strong>self</strong>?.cardTypeIndicator.isHidden = <strong>self</strong>?.cardTypeIndicator.text?.isEmpty ?? <strong>true</strong><br>}</pre><p><strong>Line 1</strong> sets up a PaymentCardRule on the card number text field with visa as the only supported type.</p><p><strong>Line 2</strong> instructs the framework to run the validation algorithm whenever the text changes in the text field.</p><p><strong>Line 3</strong> registers a validation handler to be called whenever the validation algorithm is run. If the card number field is empty, it will hide the status label. If it is not empty, the label will be set with either a green tick or red cross symbol based on validation status.</p><p>Now run the app and enter a sample visa card number;</p><pre>4111 1111 1111 1111</pre><p>See it in action below;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*jr61GUgfDOXla8V8u3NsLw.gif" /></figure><p>We are not done yet. :-) We can show the type of the card number being entered with ATGValidator. In fact, all the heavy lifting is done by the framework already, and we need to just use the value.</p><p>To disable the continue button by default, add the following lines of code to just below of <strong><em>super</em></strong><em>.viewDidLoad()</em></p><pre>continueButton.alpha = 0.5<br>continueButton.isEnabled = <strong>false</strong></pre><p>Now add .<em>mastercard </em>to the list of supported cards as given below;</p><pre>cardNumberTextField.validationRules = [<br>    PaymentCardRule(acceptedTypes: [.visa, .mastercard])<br>]</pre><p>Update the validation handler part in viewDidLoad as follows;</p><pre>cardNumberTextField.validationHandler = { [<strong>weak</strong> <strong>self</strong>] result <strong>in<br>    if</strong> <strong>self</strong>?.cardNumberTextField.text?.isEmpty ?? <strong>true</strong> {<br>        <strong>self</strong>?.cardNumberStatusLabel.text = <strong>nil<br>    </strong>} <strong>else</strong> {<br>        <strong>var</strong> statusString = (result.status == .success ? &quot;✅&quot; : &quot;❌&quot;)<br>        <strong>if</strong> <strong>let</strong> type = result.value <strong>as</strong>? PaymentCardType {<br>            statusString = type.rawValue + &quot; &quot; + statusString<br>        }<br>        <strong>self</strong>?.cardNumberStatusLabel.text = statusString<br>    }<br>    <strong>self</strong>?.cardNumberStatusLabel.isHidden =<br>    <strong>self</strong>?.cardNumberStatusLabel.text?.isEmpty ?? <strong>true<br>    self</strong>?.continueButton.isEnabled = result.status == .success<br>    <strong>self</strong>?.continueButton.alpha = result.status == .success ? 1.0 : 0.5<br>}</pre><p>Build and run the project. You can find a list of test credit cards from PayPal <a href="https://www.paypalobjects.com/en_GB/vhelp/paypalmanager_help/credit_card_numbers.htm">here</a>. Here is it in action;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*VOynesxoRYCDu6Rdetp2UA.gif" /></figure><p>Please note that the payment card rule trims all whitespace in the text field input, so you can auto-format the credit card number while entering to have a separator between every 4 digits.</p><p>Also, ATGValidator supports the following card types out of the box.</p><pre>American Express<br>MasterCard<br>Maestro<br>Visa<br>Visa Electron<br>Discover<br>Diners Club</pre><p>Hope you found this article helpful. If so, please do not forget to clap :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10514246c634" width="1" height="1" alt=""><hr><p><a href="https://medium.com/ounass/credit-debit-card-validation-and-type-detection-in-swift-using-atgvalidator-10514246c634">Credit/Debit card validation and type detection in Swift using ATGValidator</a> was originally published in <a href="https://medium.com/ounass">Ounass</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>