UniNuri (11) : 비밀번호 / 이메일 정규식(Regular Expression)
2023. 7. 30. 15:07
우리가 회원가입할 때 이메일에 @가 들어가야 되거나 비밀번호에 특수문자가 들어가 있는지 확인하는
정규식에 대하여 확인한다.
1. 이메일 형식이 맞는지 확인하는 정규식(@)
func isValidEmail(email:String?) -> Bool {
guard email != nil else {return false}
let regEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
let pred = NSPredicate(format:"SELF MATCHES %@", regEx)
return pred.evaluate(with: email)
}
2. 비밀번호에 특수문자가 있는지 확인하는 정규식
func isValidPassword(pw: String?) -> Bool {
guard pw != nil else {return false}
let regEx = "^(?=.*[A-Za-z])(?=.*[0-9])(?=.*[!@#$%^&*()_+=-]).{8,50}"
let pred = NSPredicate(format:"SELF MATCHES %@", regEx)
return pred.evaluate(with: pw)
}
3. 비밀번호를 만들 때 비밀번호 확인하는 함수
func isSamePassword(pw: String?, checkpw: String?) -> Bool{
let pw = passwordTextField.text
let checkpw = confirmPasswordTextField.text
if pw == checkpw{
return true
}else{
return false
}
}
위 세 개의 식으로 textFieldEdit 함수와 textFieldCompare 만들었다.
// Confirm Email, Password
@objc func textFieldEdited(textField: UITextField){
if textField == emailTextField{
if emailTextField.text?.isEmpty == false{
if isValidEmail(email: textField.text) == true{
isValidEmailLabel.heightAnchor.constraint(equalToConstant: 0).isActive = true
}
else{
isValidEmailLabel.heightAnchor.constraint(equalToConstant: 15).isActive = true
isValidEmailLabel.text = "The email format is not valid."
isValidEmailLabel.textColor = .red
}
}
else{
isValidEmailLabel.heightAnchor.constraint(equalToConstant: 0).isActive = true
}
}
else if textField == passwordTextField{
if isValidPassword(pw: textField.text) == true{
isValidPasswordLabel.heightAnchor.constraint(equalToConstant: 0).isActive = true
}
else{
isValidPasswordLabel.heightAnchor.constraint(equalToConstant: 15).isActive = true
isValidPasswordLabel.text = "The password format is not valid."
isValidPasswordLabel.textColor = .red
}
}
}
@objc func textFieldCompare(textField: UITextField){
if textField == confirmPasswordTextField{
if isSamePassword(pw: passwordTextField.text, checkpw: confirmPasswordTextField.text) == true{
isCorrectPasswordLabel.heightAnchor.constraint(equalToConstant: 15).isActive = true
isCorrectPasswordLabel.text = "Password is correct"
isCorrectPasswordLabel.textColor = .green
}
else{
isCorrectPasswordLabel.heightAnchor.constraint(equalToConstant: 15).isActive = true
}
}
}
728x90
'개발일지' 카테고리의 다른 글
TripMate (2) - 주변 지역 검색 API (0) | 2023.12.06 |
---|---|
TripMate (1) - openweatherAPI와 CLLocationManager 사용하기 (0) | 2023.11.21 |
UniNuri (10) : 대학교 API 불러오기, UISearchControllor (0) | 2023.07.09 |
UniNuri (9) : Amplify Authentication (0) | 2023.07.02 |
UniNuri (8) : APNs, Authentication Setting (0) | 2023.06.25 |