Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/dme/update email validation regex #28

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 10 additions & 11 deletions AnimatedField/Classes/AnimatedField+TextFieldDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,16 @@ extension AnimatedField: UITextFieldDelegate {

// Copy new character
var newInput = string

// Replace special characters in newInput
newInput = newInput.replacingOccurrences(of: "`", with: "")
newInput = newInput.replacingOccurrences(of: "^", with: "")
newInput = newInput.replacingOccurrences(of: "¨", with: "")

// Replace special characters in textField
textField.text = textField.text?.replacingOccurrences(of: "`", with: "")
textField.text = textField.text?.replacingOccurrences(of: "^", with: "")
textField.text = textField.text?.replacingOccurrences(of: "¨", with: "")


let invalidCharacters = format.invalidCharacters.map { String($0) }
for invalidCharacter in invalidCharacters {
// Replace special characters in newInput
newInput = newInput.replacingOccurrences(of: invalidCharacter, with: "")

// Replace special characters in textField
textField.text = textField.text?.replacingOccurrences(of: invalidCharacter, with: "")
}

// Apply uppercased & lowercased if available
if uppercased { newInput = newInput.uppercased() }
if lowercased { newInput = newInput.lowercased() }
Expand Down
21 changes: 10 additions & 11 deletions AnimatedField/Classes/AnimatedField+TextViewDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ extension AnimatedField: UITextViewDelegate {

// Copy new character
var newInput = text

// Replace special characters in newInput
newInput = newInput.replacingOccurrences(of: "`", with: "")
newInput = newInput.replacingOccurrences(of: "^", with: "")
newInput = newInput.replacingOccurrences(of: "¨", with: "")

// Replace special characters in textView
textView.text = textView.text?.replacingOccurrences(of: "`", with: "")
textView.text = textView.text?.replacingOccurrences(of: "^", with: "")
textView.text = textView.text?.replacingOccurrences(of: "¨", with: "")


let invalidCharacters = format.invalidCharacters.map { String($0) }
for invalidCharacter in invalidCharacters {
// Replace special characters in newInput
newInput = newInput.replacingOccurrences(of: invalidCharacter, with: "")

// Replace special characters in textView
textView.text = textView.text?.replacingOccurrences(of: invalidCharacter, with: "")
}

// Apply uppercased & lowercased if available
if uppercased { newInput = newInput.uppercased() }
if lowercased { newInput = newInput.lowercased() }
Expand Down
23 changes: 21 additions & 2 deletions AnimatedField/Classes/AnimatedField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ open class AnimatedField: UIView {
didSet { textField.keyboardType = keyboardType }
}

public var returnKeyType = UIReturnKeyType.default {
didSet { textField.returnKeyType = returnKeyType }
}

public var keyboardToolbar: UIToolbar? {
didSet { textField.inputView = keyboardToolbar }
}
Expand Down Expand Up @@ -317,11 +321,20 @@ open class AnimatedField: UIView {
}

private func setupDatePicker(mode: UIDatePicker.Mode?, minDate: Date?, maxDate: Date?, chooseText: String?) {
datePicker = UIDatePicker()
datePicker = UIDatePicker(frame: .zero)
datePicker?.datePickerMode = mode ?? .date
datePicker?.maximumDate = maxDate
datePicker?.minimumDate = minDate
datePicker?.setValue(format.textColor, forKey: "textColor")
if #available(iOS 13.4, *) {
if #available(iOS 14.0, *) {
datePicker?.preferredDatePickerStyle = .inline
} else {
datePicker?.preferredDatePickerStyle = .wheels
}
} else {
// Fallback on earlier versions
}

let toolBar = UIToolbar(target: self, selector: #selector(didChooseDatePicker))

Expand All @@ -346,7 +359,13 @@ open class AnimatedField: UIView {
textField.inputAccessoryView = accessoryView ?? toolBar
textField.inputView = numberPicker
}


open override var isFirstResponder: Bool {
get {
return textField.isFirstResponder
}
}

open override func becomeFirstResponder() -> Bool {
textField.becomeFirstResponder()
return super.becomeFirstResponder()
Expand Down
12 changes: 6 additions & 6 deletions AnimatedField/Classes/AnimatedField.xib
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14868" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="17147" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14824"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17120"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand Down Expand Up @@ -32,9 +32,9 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="hXt-nZ-E0Z">
<rect key="frame" x="0.0" y="20" width="271" height="30"/>
<rect key="frame" x="0.0" y="20" width="271" height="34"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="jwF-CT-fSa"/>
<constraint firstAttribute="height" constant="34" id="jwF-CT-fSa"/>
</constraints>
<color key="textColor" white="0.33333333329999998" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<fontDescription key="fontDescription" name="AvenirNext-Regular" family="Avenir Next" pointSize="16"/>
Expand All @@ -43,7 +43,7 @@
<action selector="didChangeTextField:" destination="-1" eventType="editingChanged" id="qNr-An-YQ3"/>
</connections>
</textField>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="6tZ-aE-LZA">
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="system" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="6tZ-aE-LZA">
<rect key="frame" x="241" y="20" width="30" height="30"/>
<constraints>
<constraint firstAttribute="width" constant="30" id="OhA-pe-BTf"/>
Expand Down Expand Up @@ -100,6 +100,7 @@
<textInputTraits key="textInputTraits" autocapitalizationType="sentences" autocorrectionType="no"/>
</textView>
</subviews>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="hXt-nZ-E0Z" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" id="6f3-g8-AFi"/>
Expand All @@ -126,7 +127,6 @@
<constraint firstItem="xJJ-jF-Qy7" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="20" id="z6S-Ye-Vga"/>
</constraints>
<freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
<viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
<point key="canvasLocation" x="-18.399999999999999" y="250.9745127436282"/>
</view>
</objects>
Expand Down
7 changes: 5 additions & 2 deletions AnimatedField/Classes/AnimatedFieldFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import Foundation

public struct AnimatedFieldFormat {


/// String of characters to block/filter from input
public var invalidCharacters = "`^¨"

/// Title always visible
public var titleAlwaysVisible = false

/// Font for title label
public var titleFont = UIFont.systemFont(ofSize: 13, weight: .regular)

Expand Down
4 changes: 2 additions & 2 deletions AnimatedField/Classes/AnimatedFieldType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum AnimatedFieldType {

var typingExpression: String {
switch self {
case .email: return "[A-Z0-9a-z@_\\.]"
case .email: return "[A-Z0-9a-z@_\\.\\-]"
case .username: return "[A-Za-z0-9_.]"
case .price: return "[0-9\(decimal)]"
default: return ".*"
Expand All @@ -37,7 +37,7 @@ public enum AnimatedFieldType {

var validationExpression: String {
switch self {
case .email: return "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
case .email: return "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
case .username(let min, let max): return "[A-Za-z0-9_.]{\(min),\(max)}"
case .password(let min, let max): return ".{\(min),\(max)}"
case .price(_, let max): return "^(?=.*[1-9])([1-9]\\d*(?:\(decimal)\\d{1,\(max)})?|(?:0\(decimal)\\d{1,\(max)}))$"
Expand Down