Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Example/Kommunicate/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ PreChatViewEmailAndPhoneNumberEmptyError = "Please fill email or the phone numbe
PreChatViewEmailInvalidError = "Please enter correct email address";
/* Invalid Phone number error message */
PreChatViewPhoneNumberInvalidError = "Please enter correct phone number";
/* Invalid Email error message for custom regex */
PreChatViewEmailRegexFailedError = "Please enter valid email address";
/* Invalid Name error message for custom regex */
PreChatViewNameRegexFailedError = "Please enter valid name";
/* Invalid Password error message for custom regex */
PreChatViewPasswordRegexFailedError = "Please enter valid password";
/* Empty Name error message */
PreChatViewNameEmptyError = "Please enter your name";
/* Empty Password error message */
Expand Down
75 changes: 62 additions & 13 deletions Sources/Kommunicate/Classes/KMPreChatFormViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ open class KMPreChatFormViewController: UIViewController {
/// user has submitted. By default, it's nil.
/// When it's nil, we use `NSDataDetector` to validate the phone number.
public var phoneNumberRegexPattern: String?

/// The regular expression pattern that will be used to validate the name
/// user has entered. By default, it's nil.
/// When it's nil, basic non-empty string validation can be applied.
public var nameRegexPattern: String?

/// The regular expression pattern that will be used to validate the email
/// user has submitted. By default, it's nil.
/// When it's nil, default email format validation will be used.
public var emailRegexPattern: String?

/// The regular expression pattern that will be used to validate the password
/// user has submitted. By default, it's nil.
/// When it's nil, fallback password validation may be applied.
public var passwordRegexPattern: String?

public init() {}
}
Expand All @@ -61,6 +76,9 @@ open class KMPreChatFormViewController: UIViewController {
enum TextFieldValidationError: Error, Localizable {
case emailAndPhoneNumberEmpty
case invalidEmailAddress
case invalidCustomEmailRegex
case invalidCustomNameRegex
case invalidCustomPasswordRegex
case invalidPhoneNumber
case emptyName
case emptyEmailAddress
Expand All @@ -85,6 +103,12 @@ open class KMPreChatFormViewController: UIViewController {
return localizedString(forKey: "PreChatViewPhoneNumberEmptyError", fileName: fileName)
case .emptyPassword:
return localizedString(forKey: "PreChatViewPasswordEmptyError", fileName: fileName)
case .invalidCustomEmailRegex:
return localizedString(forKey: "PreChatViewEmailRegexFailedError", fileName: fileName)
case .invalidCustomNameRegex:
return localizedString(forKey: "PreChatViewNameRegexFailedError", fileName: fileName)
case .invalidCustomPasswordRegex:
return localizedString(forKey: "PreChatViewPasswordRegexFailedError", fileName: fileName)
}
}
}
Expand Down Expand Up @@ -291,32 +315,57 @@ open class KMPreChatFormViewController: UIViewController {
outerLoop: for mandatoryOption in preChatConfiguration.mandatoryOptions {
switch mandatoryOption {
case .email:
if let emailText = emailTextField.text,
!emailText.isEmpty, !emailText.isValidEmail {
validationError = TextFieldValidationError.invalidEmailAddress
guard let emailText = emailTextField.text, !emailText.isEmpty else {
validationError = .invalidEmailAddress
break outerLoop
}

if !emailText.isValidEmail {
validationError = .invalidEmailAddress
break outerLoop
}

if let pattern = preChatConfiguration.emailRegexPattern,
!emailText.matchesWithPattern(pattern) {
validationError = .invalidCustomEmailRegex
break outerLoop
}

case .name:
if let nameText = nameTextField.text, nameText.isEmpty {
validationError = TextFieldValidationError.emptyName
guard let nameText = nameTextField.text, !nameText.isEmpty else {
validationError = .emptyName
break outerLoop
}

if let pattern = preChatConfiguration.nameRegexPattern,
!nameText.matchesWithPattern(pattern) {
validationError = .invalidCustomNameRegex
break outerLoop
}

case .password:
if let passwordText = passwordTextField.text, passwordText.isEmpty {
validationError = TextFieldValidationError.emptyPassword
guard let passwordText = passwordTextField.text, !passwordText.isEmpty else {
validationError = .emptyPassword
break outerLoop
}

if let pattern = preChatConfiguration.passwordRegexPattern,
!passwordText.matchesWithPattern(pattern) {
validationError = .invalidCustomPasswordRegex
break outerLoop
}

case .phoneNumber:
let isValidNumber: ((String) -> Bool) = { number in
self.preChatConfiguration.phoneNumberRegexPattern != nil ?
number.matchesWithPattern(self.preChatConfiguration.phoneNumberRegexPattern ?? "") : number.isValidPhoneNumber
guard let phoneText = phoneNumberTextField.text, !phoneText.isEmpty else {
continue // optional empty allowed
}

if let phoneNumberText = phoneNumberTextField.text,
!phoneNumberText.isEmpty, !isValidNumber(phoneNumberText) {
validationError = TextFieldValidationError.invalidPhoneNumber
let isValid = preChatConfiguration.phoneNumberRegexPattern.map {
phoneText.matchesWithPattern($0)
} ?? phoneText.isValidPhoneNumber

if !isValid {
validationError = .invalidPhoneNumber
break outerLoop
}
}
Expand Down
5 changes: 1 addition & 4 deletions Sources/Resources/Assets/KMPreChatUserFormView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,8 @@
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="top" translatesAutoresizingMaskIntoConstraints="NO" id="SM4-Ob-Uc2">
<rect key="frame" x="0.0" y="494" width="345" height="76"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="3" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WpO-nN-HX8">
<label opaque="NO" userInteractionEnabled="YES" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="" textAlignment="center" lineBreakMode="wordWrap" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="WpO-nN-HX8">
<rect key="frame" x="0.0" y="0.0" width="345" height="40"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="lWF-bt-dxh"/>
</constraints>
<fontDescription key="fontDescription" name="HelveticaNeue-Medium" family="Helvetica Neue" pointSize="16"/>
<color key="textColor" red="1" green="0.14913141730000001" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<nil key="highlightedColor"/>
Expand Down
6 changes: 6 additions & 0 deletions Sources/Resources/Assets/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ PreChatViewEmailAndPhoneNumberEmptyError = "Please fill email or the phone numbe
PreChatViewEmailInvalidError = "Please enter correct email address";
/* Invalid Phone number error message */
PreChatViewPhoneNumberInvalidError = "Please enter correct phone number";
/* Invalid Email error message for custom regex */
PreChatViewEmailRegexFailedError = "Please enter valid email address";
/* Invalid Name error message for custom regex */
PreChatViewNameRegexFailedError = "Please enter valid name";
/* Invalid Password error message for custom regex */
PreChatViewPasswordRegexFailedError = "Please enter valid password";
/* Empty Name error message */
PreChatViewNameEmptyError = "Please enter your name";
/* Empty Email error message */
Expand Down
Loading