Jurian Amatsahip
Jurian Amatsahip

Reputation: 187

Error with swift regex using swiftvalidator?

I'm using the swiftValdator framework for iOS development. I created a custom validator class with a regex.

The error is:

'NSInternalInconsistencyException', reason: 'Can't do regex matching, reason: Can't open pattern U_REGEX_MISSING_CLOSE_BRACKET (string lol, pattern ^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[,.:=+-_|)(%$#@!£/?';&"\])[A-Za-z0-9,.:=+-_|)(%$#@!]£/?';&"\]{8,32}, case 0, canon 0)'

This is my code:

import Foundation
import SwiftValidator

class CustomPasswordRule: RegexRule {
    //^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[,.:=+\-_|)(%$#@!£/?`;&"\\])[A-Za-z\d,.:=+\-_|)(%$#@!£/?`;&"\\]{8,32}
    static let regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[,.:=+-_|)(%$#@!£/?';&\"\\])[A-Za-z0-9,.:=+-_|)(%$#@!]£/?';&\"\\]{8,32}"

    convenience init(message: String = "Not a valid Password") {
        self.init(regex: CustomPasswordRule.regex,message: message)
    } 
}

Can anyone help me with the error? These are the conditions:

  1. Password must be between 8 and 32 characters in length.
  2. Password must contain any 3 of the following characters combinations:
    • 1 Uppercase (capital) letter [A-Z]
    • 1 Lowercase (common) letter [a-z]
    • 1 Number [0-9]
    • 1 Symbol [, . : = + - _ | ) ( % $ # @ ! £ / \ ? ` ; & "]

Upvotes: 1

Views: 375

Answers (1)

Bryan Cimo
Bryan Cimo

Reputation: 1308

The regex inside the "" isn't converted correctly, try this:

static let regex = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[,.:=+\\-_|)(%$#@!£/?`;&\"\\\\])[A-Za-z\\d,.:=+\\-_|)(%$#@!£/?`;&\"\\\\]{8,32}"

Upvotes: 1

Related Questions