wuhuang 2017-06-15
转自 : http://blog.csdn.net/h643342713/article/details/54292935
//只能为中文 func onlyInputChineseCharacters(_ string: String) -> Bool { var inputString = "[\u{4e00}-\u{9fa5}]+" var predicate = NSPredicate(format: "SELF MATCHES %@", inputString) var Chinese = predicate.evaluate(withObject: string) return Chinese } //只能为数字 func onlyInputTheNumber(_ string: String) -> Bool { var numString = "[0-9]*" var predicate = NSPredicate(format: "SELF MATCHES %@", numString) var number = predicate.evaluate(withObject: string) return number } //只能为小写 func onlyInputLowercaseLetter(_ string: String) -> Bool { var regex = "[a-z]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var letter = predicate.evaluate(withObject: string) return letter } //只能为大写 func onlyInputACapital(_ string: String) -> Bool { var regex = "[A-Z]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var capital = predicate.evaluate(withObject: string) return capital } //允许大小写 func inputCapitalAndLowercaseLetter(_ string: String) -> Bool { var regex = "[a-zA-Z]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许大小写或数字(不限字数) func inputLettersOrNumbers(_ string: String) -> Bool { var regex = "[a-zA-Z0-9]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许大小写或数字(限字数) func inputNumberOrLetters(_ name: String) -> Bool { var userNameRegex = "^[A-Za-z0-9]{6,20}+$" var userNamePredicate = NSPredicate(format: "SELF MATCHES %@", userNameRegex) var inputString = userNamePredicate.evaluate(withObject: name) return inputString } //允许汉字,大小写或数字(限字数) func inputChineseOrLettersNumberslimit(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}]+[A-Za-z0-9]{6,20}+$" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许汉字或数字(不限字数) func inputChineseOrNumbers(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}]+[0-9]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许汉字或数字(限字数) func inputChineseOrNumbersLimit(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}][0-9]{6,20}+$" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许汉字,大小写或数字(不限字数) func inputChineseOrLettersAndNumbersNum(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}]+[A-Za-z0-9]*" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString } //允许汉字,大小写或数字(限字数) func inputChineseOrLettersNumberslimit(_ string: String) -> Bool { var regex = "[\u{4e00}-\u{9fa5}]+[A-Za-z0-9]{6,20}+$" var predicate = NSPredicate(format: "SELF MATCHES %@", regex) var inputString = predicate.evaluate(withObject: string) return inputString }