W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/19/2021

Email & Phone Number Validation in Swift

 Different type of validation in swift, Like Email validation, Phone Number Validation , Pincode Validation, Password validation, Password length validation, Here all types of validation code explain and how can you use it in code.

Email Validation :

1234567func isValidEmail(testStr:String) -> Bool {
    print("validate emilId: \(testStr)")
    let emailRegEx = "^(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?(?:(?:(?:[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+(?:\\.[-A-Za-z0-9!#$%&’*+/=?^_'{|}~]+)*)|(?:\"(?:(?:(?:(?: )*(?:(?:[!#-Z^-~]|\\[|\\])|(?:\\\\(?:\\t|[ -~]))))+(?: )*)|(?: )+)\"))(?:@)(?:(?:(?:[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)(?:\\.[A-Za-z0-9](?:[-A-Za-z0-9]{0,61}[A-Za-z0-9])?)*)|(?:\\[(?:(?:(?:(?:(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))\\.){3}(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))))|(?:(?:(?: )*[!-Z^-~])*(?: )*)|(?:[Vv][0-9A-Fa-f]+\\.[-A-Za-z0-9._~!$&'()*+,;=:]+))\\])))(?:(?:(?:(?: )*(?:(?:(?:\\t| )*\\r\\n)?(?:\\t| )+))+(?: )*)|(?: )+)?$"
    let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    let result = emailTest.evaluate(with: testStr)
    return result
}

Phone Number Validation :

12345func validate(value: String) -> Bool {
    let PHONE_REGEX = "^((\\+)|(00))[0-9]{6,14}$"        let phoneTest = NSPredicate(format: "SELF MATCHES %@", PHONE_REGEX)
    let result =  phoneTest.evaluate(with: value)
    return result
}

Pincode Validation :

12345678func isValidPincode(value: String) -> Bool {
    if value.characters.count == 6{
      return true
    }
    else{
      return false
    }
}

Password Validation : Check current and Confirm is Same.

1234567func isPasswordSame(password: String , confirmPassword : String) -> Bool {
    if password == confirmPassword{
      return true
    }else{
      return false
    }
}

Password length Validation - Length should grater than 7.

1234567func isPwdLenth(password: String , confirmPassword : String) -> Bool {
        if password.characters.count <= 7 && confirmPassword.characters.count <= 7{
           return true
        }else{
           return false
        }
    }

 how to use this function in code : 
12345if isValidEmail(testStr: "kirit@gmail.com"){
    print("Validate EmailID")
}else{
 print("invalide EmailID")
}


Thanks.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.