-
-
Save JohnnyD1776/9a03624a6ed1f06f8e06d06a059a81d0 to your computer and use it in GitHub Desktop.
String extension with separate function to check validity of a string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // StringType.swift | |
| // | |
| // Created by Johnny1776 on 25/08/2023. | |
| // | |
| import SwiftUI | |
| extension String { | |
| /** | |
| Checks whether the string falls within a specified range and matches a particular pattern. | |
| - Parameters: | |
| - range: A closed range defining the minimum and maximum length of the string. Defaults to `2...200`. | |
| - type: The `StringType` defining the pattern the string must match. Defaults to `.paragraph`. | |
| - Returns: Returns `true` if the string's length falls within the given range and matches the specified pattern. Returns `false` otherwise. | |
| */ | |
| func isValid(range: ClosedRange<Int> = 2...200, type: StringType) -> Bool { | |
| guard range.contains(self.count) else { return false } | |
| return self.range(of: type.pattern, options: .regularExpression) != nil | |
| } | |
| } | |
| /** | |
| Checks whether a given string falls within a specified range and matches a particular pattern. | |
| This function can be used to validate various string types such as password, username, name, title, age, number, phone number, paragraph, and email. | |
| - Parameters: | |
| - range: A closed range defining the minimum and maximum length of the string. Defaults to `2...200`. | |
| - type: The `StringType` defining the pattern the string must match. Defaults to `.paragraph`. | |
| - value: The string to be checked. | |
| - Returns: Returns `true` if the string's length falls within the given range and matches the specified pattern. Returns `false` otherwise. | |
| - Example Usage: | |
| - Validate a Password: `checkString(range: 8...16, type: .password, value: "Password123!")` | |
| - Validate an Email: `checkString(type: .email, value: "john.doe@example.com")` | |
| - Validate a Phone Number with Custom Length: `checkString(range: 10...15, type: .phoneNumber, value: "123-456-7890")` | |
| - Note: The patterns for each `StringType` are defined in the `StringType` enumeration. Modify these patterns to suit specific requirements. | |
| */ | |
| func checkString(range: ClosedRange<Int> = 2...200, type: StringType = .paragraph, value: String) -> Bool { | |
| guard range.contains(value.count) else { return false } | |
| return value.range(of: type.pattern, options: .regularExpression) != nil | |
| } | |
| enum StringType { | |
| case password | |
| case username | |
| case name | |
| case title | |
| case age | |
| case number | |
| case phoneNumber | |
| case paragraph | |
| case email | |
| var pattern:String { | |
| switch self { | |
| case .age: | |
| return "^[0-9]+$" | |
| case .name: | |
| return "^[A-Za-z\\s-']+$" | |
| case .email: | |
| return #"^\S+@\S+\.\S+$"# | |
| case .number: | |
| return "^[0-9]+$" | |
| case .paragraph: | |
| return "^.*$" | |
| case .password: | |
| let passwordPattern = | |
| // At least 8 characters | |
| #"(?=.{8,})"# + | |
| // At least one capital letter | |
| #"(?=.*[A-Z])"# + | |
| // At least one lowercase letter | |
| #"(?=.*[a-z])"# + | |
| // At least one digit | |
| #"(?=.*\d)"# + | |
| // At least one special character | |
| #"(?=.*[ !$%&?._-])"# | |
| return passwordPattern | |
| case .phoneNumber: | |
| return #"^\(?\d{3}\)?[ -]?\d{3}[ -]?\d{4}$"# | |
| case .title: | |
| return "^[A-Za-z0-9\\s.,!?]+$" | |
| case .username: | |
| return #"^[a-zA-Z][a-zA-Z0-9_-]*$"# | |
| } | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment