swift 常见输入限制
1、数字+输入位数 判断
extension 自己的VC或view : UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
if string.count == 0 {
return true
}
let existedLength = textField.text?.count
let selectedLength = range.length
let replaceLength = string.count
//判断是纯数字
var numberRes = true
var lenghtBool = true
let tmSet = CharacterSet(charactersIn:"0123456789")
var i = 0
while i < string.count {
let string1:NSString = (string as NSString).substring(with: NSMakeRange(i, 1)) as NSString
let range = string1.rangeOfCharacter(from: tmSet)
if (range.length == 0) {
numberRes = false
break
}
i += 1
}
if existedLength! - selectedLength + replaceLength > 11{
lenghtBool = false
}else{
lenghtBool = true
}
//MARK:---输入框只能输入数字 并且长度是 11位
if self.selectType == 1 {
if textField == self.phoneTF {
if numberRes == true && lenghtBool == true {
return true
}else{
return false
}
}
}
return true
}
}
2、输入金额 控制小数点后两位 如:.xx
extension 自己的VC或View: UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let existedLength = textField.text?.count
let selectedLength = range.length
let replaceLength = string.count
if existedLength! - selectedLength + replaceLength > 12 {
return false
}
//控制小数点后两位 .xx
let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
//正则匹配
let expression = "^[0-9]*((\\.|,)[0-9]{0,2})?$" //^[0-9]*((\\.|,)[0-9]{0,2})?$
let regex = try! NSRegularExpression(pattern: expression, options:.allowCommentsAndWhitespace)
let numberOfMatches = regex.numberOfMatches(in: newString, options: .reportProgress, range: NSMakeRange(0, (newString as NSString).length))
return numberOfMatches != 0
}
}
3、输入限制: 数字+字母+输入长度
extension 自己的VC或view : UITextFieldDelegate {
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
if string.count == 0 {
return true
}
///---------------1.判断输入数字 或 判断输入字母
var ziMuFlag = true
let ziMuSet = CharacterSet(charactersIn:"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var p = 0
while p < string.count {
let string1:NSString = (string as NSString).substring(with: NSMakeRange(p, 1)) as NSString
let range = string1.rangeOfCharacter(from: ziMuSet)
if (range.length == 0) {
ziMuFlag = false
break
}
p += 1
}
print("--ziMuFlag---\(ziMuFlag)")
///-----------------2.输入长度限制
let existedLength = textField.text?.count
let selectedLength = range.length
let replaceLength = string.count
//输入长度控制
var lenghtBool = true
if existedLength! - selectedLength + replaceLength > 11{
lenghtBool = false
}else{
lenghtBool = true
}
if ziMuFlag == true && lenghtBool == true {
return true
}else{
return false
}
}
}
4、身份证输入判断
extension 自己的VC或View :UITextFieldDelegate{
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
if string.count == 0 {
return true
}
let existedLength = textField.text?.count
let selectedLength = range.length
let replaceLength = string.count
///------------1.判断纯数字
var numberRes = true
let tmSet = CharacterSet(charactersIn:"0123456789")
var i = 0
while i < string.count {
let string1:NSString = (string as NSString).substring(with: NSMakeRange(i, 1)) as NSString
let range = string1.rangeOfCharacter(from: tmSet)
if (range.length == 0) {
numberRes = false
break
}
i += 1
}
///-----------------2.判断输入字母
var ziMuFlag = true
let ziMuSet = CharacterSet(charactersIn:"xX")
var p = 0
while p < string.count {
let string1:NSString = (string as NSString).substring(with: NSMakeRange(p, 1)) as NSString
let range = string1.rangeOfCharacter(from: ziMuSet)
if (range.length == 0) {
ziMuFlag = false
break
}
p += 1
}
print("--ziMuFlag---\(ziMuFlag)")
//----------3、判断输入长度
var IdCardlenghtBool = true
if existedLength! - selectedLength + replaceLength > 18 {
IdCardlenghtBool = false
}else{
IdCardlenghtBool = true
}
///身份证校验
var idCardRes = true
if numberRes == true || ziMuFlag == true {
idCardRes = true
}else{
idCardRes = false
}
//MARK:---身份证号 只能输入规则 (纯数字 || xX ) + 长度(最长)18位
if textField == self.idCardTF {
if idCardRes == true && IdCardlenghtBool == true {
return true
}else{
return false
}
}
return true
}
}
以上是自己在项目中亲自用到的,都很实用,希望对有相同需求的朋友有帮助。