๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“Œ Language/Swift

Swift ์ด๋ฉ”์ผ ํ˜•์‹ ๊ฒ€์‚ฌํ•˜๊ธฐ(Email Validation)

by Fomagran ๐Ÿ’ป 2020. 1. 11.
728x90
๋ฐ˜์‘ํ˜•

์ด๋ฉ”์ผ ํ˜•์‹์„ ๊ฒ€์‚ฌํ•˜๋ ค๋ฉด ์ •๊ทœํ‘œํ˜„์‹(Regular Expression)์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ์ง€์‹์ด ์žˆ์–ด์•ผ ํ–ˆ๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ฉ”์ผ validation์ด๋‚˜ ํŒจ์Šค์›Œ๋“œ validation์„ ์น˜๋ฉด ๋‹ค์–‘ํ•œ ์˜ˆ์ œ๊ฐ€ ๋‚˜์˜ค๋‹ˆ๊น ์ผ๋‹จ์€ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด ์กฐ๊ธˆ๋งŒ ์ดํ•ดํ•˜๊ณ  ๋‚˜์ค‘์— ์ •๊ทœํ‘œํ˜„์‹์„ ๊ณต๋ถ€ํ•˜๋„๋ก ํ•˜์ž.

์šฐ์„  ์ด๋ฉ”์ผ ๊ฒ€์‚ฌ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋ฐ›์•„์˜ฌ ๋ฌธ์ž์—ด์„ ๋„ฃ์–ด์ค€๋‹ค.

์šฐ์„  ๊ฐ„๋‹จํ•˜๊ฒŒ let emailRegEx๋ฅผ ์„ค๋ช…ํ•˜๋ฉด [A-Z0-9a-z._%+-]์€ ์˜์–ด ๋Œ€๋ฌธ์ž ์†Œ๋ฌธ์ž ํŠน์ˆ˜๋ฌธ์ž ๋ชจ๋‘ ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋œป์ด๊ณ 

+@ ๋Š” ์‚ฌ์ด์— ๋ฌด์กฐ๊ฑด @๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

@๋’ค์—๋Š” [A-Za-z0-9.-] ๋Œ€๋ฌธ์ž ์†Œ๋ฌธ์ž ์ˆซ์ž .-๋งŒ ๋˜๊ณ  ๊ทธ ๋‹ค์Œ์—

.์ด ์˜จ ์ดํ›„ [A-Za-z]๋Š” ์˜์–ด ๋Œ€๋ฌธ์ž ์†Œ๋ฌธ์ž๋งŒ ๊ฐ€๋Šฅ

{2,64}๋Š” 2~64๊ธ€์ž๋งŒ ํ—ˆ์šฉํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.

 func isValidEmail(testStr:String) -> Bool {
       let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
       let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
       return emailTest.evaluate(with: testStr)
        }

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€