Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

개발여행일지~

회원가입시 조건과 비밀번호 입력시 제한사항 적용하기 본문

코틀린 공부정리

회원가입시 조건과 비밀번호 입력시 제한사항 적용하기

야생돌고래 2023. 8. 17. 21:06

회원가입을 하였을 때 각각에 EditText에 조건을 넣어보자!

 

이름은 4글자 이내, 아이디는 20자 이내, 비밀번호는 영문, 숫자, 특수문자 8~16자 이내로 작성했을 때 회원가입이 가능하게 

조건을 넣어보았다.

 

package com.example.sangbusanzo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import java.util.regex.Pattern

class SignInPage : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_signinpage)

            val btnCancel = findViewById<Button>(R.id.cancel_button)
        btnCancel.setOnClickListener {
            finish() // 취소버튼을 누르면 로그인페이지로 이동
        }

            val btn_add = findViewById<Button>(R.id.btn_signup)
            btn_add.setOnClickListener {
                val textname = findViewById<EditText>(R.id.edit_name)
                val text_Id = findViewById<EditText>(R.id.edittextId)
                val text_Pass = findViewById<EditText>(R.id.editTextPw)
                val username = textname.text.toString()
                val userId = text_Id.text.toString()
                val password = text_Pass.text.toString()

                var hasError = false

                if (username.length > 4) {
                    textname.error = "이름은 4자 이내로 입력해주세요."
                    hasError = true
                }

                if (userId.length > 20) {
                    text_Id.error = "아이디는 20자 이내로 입력해주세요."
                    hasError = true
                }

                if (password.length < 8 || password.length > 16) {
                    text_Pass.error = "비밀번호는 영문, 숫자, 특수문자를 조합하여 8자에서 16자 사이로 입력해주세요."
                    hasError = true
                } else if (!isValidPassword(password)) {
                    text_Pass.error = "비밀번호는 영어, 숫자, 특수문자만 사용하여 입력해주세요."
                    hasError = true
                }

                if (!hasError) {
                    Toast.makeText(this, "회원가입 성공!!", Toast.LENGTH_SHORT).show()

                    val resultIntent = Intent()
                    resultIntent.putExtra("dataFromSignUpActivity", userId)
                    resultIntent.putExtra("passwordFromSignUpActivity", password)
                    resultIntent.putExtra("nameFromSignUpActivity", username)
                    setResult(RESULT_OK, resultIntent)
                    finish()
                }
            }
    }
        private fun isValidPassword(password: String): Boolean {
            val pattern = Pattern.compile("^[a-zA-Z0-9!@#\$%^&*()_+\\-=\\[\\]{};':\"\\\\|,.<>\\/?]*$")
            return pattern.matcher(password).matches()
        }
}

 

위와 같이 각각의 Text에 조건을 주고 조건이 맞지않을시 에러메시지를 띄우게 설정하였다.

그리고 비밀번호Text에는 특정 패턴을 줘서 다른 패턴은 입력되지 않게 하였다.

위 사진과 같이 이름은 4글자를 넘었고, 비밀번호는 8~16자 이내에 패스워드를 적지 않아 경고문 구을 띄웠다.

조건을 잘지켰다면 위 이미지와 같이 회원가입 성공이라는 문구와 함께 자동으로

로그인페이지에 들어가 있는 것을 볼 수 있다.