프로그램 개발서

[Jquery][CodeIgniter] 회원가입 관련 코드 모음. 본문

Web

[Jquery][CodeIgniter] 회원가입 관련 코드 모음.

rairen 2020. 5. 20. 10:33

1. SignUp.js

	var id_check = false;
	$(document).on('blur', 'input[name="id"]', function () {
		var id_check_text = $('.id_check_text');
		if (this.value.length >= 4) {
			var pattern1 = /[0-9]/;
			var pattern2 = /[a-z]/;
			var pattern_count = 0;
			if (pattern1.test(this.value)) {
				pattern_count++;
			}
			if (pattern2.test(this.value)) {
				pattern_count++;
			}
			if (pattern_count === 1 || pattern_count === 2) {
				var f = $(this).closest('form');
				var id = this.value;
				$.ajax({
					url: [처리 주소],
					type: 'post',
					data: {
						id: id,
						pdx_csrf_token: $('input[name="' + csrf + '"]').val(),
					},
					dataType: 'json',
					success: function (result) {
						if (result.code === 'success') {
							id_check_text.addClass('text-danger').text(result.message);
							id_check = false;
						}
						else if (result.code === 'fail') {
							id_check_text.addClass('text-success').text(result.message);
							id_check = true;
						}
						else {
							id_check_text.addClass('text-danger').text('ERROR : AJAX');
							id_check = false;
						}
						f.find('input[name="' + result.data.csrf.name + '"]').val(result.data.csrf.hash);
					},
					error: function (error) {
						console.log(error);
						id_check = false;
					},
				});
			}
			else {
				id_check_text.addClass('text-danger').text('영어 소문자, 숫자 외의 문자는 아이디로 이용하실 수 없습니다.');
				id_check = false;
			}
		}
		else if (this.value.length >= 1 && this.value.length < 4) {
			id_check_text.addClass('text-danger').text('4자 이상으로 입해야합니다.');
			id_check = false;
		}
		else {
			id_check_text.addClass('text-danger').text('아이디는 필 수 입니다.');
			id_check = false;
		}
	});

2. Process

	public function sign_up()
	{
		$is_database = TRUE;
		$id          = $this->input->post('id', TRUE);
		if (is_null($id))
		{
			$this->result = array(
				'code'    => 'fail',
				'message' => '아이디를 입력하시기 바랍니다..',
				'data'    => array(
					'csrf' => $this->data['csrf']
				)
			);
			$is_database  = FALSE;
		}
		if (strlen($id) < 4)
		{
			$this->result = array(
				'code'    => 'fail',
				'message' => '아이디는 4자 이상 입력해야합니다.',
				'data'    => array(
					'csrf' => $this->data['csrf']
				)
			);
			$is_database  = FALSE;
		}
		$password = $this->input->post('password', TRUE);
		if (is_null($password))
		{
			$this->result = array(
				'code'    => 'fail',
				'message' => '비밀번호를 입력하시 바랍니다..',
				'data'    => array(
					'csrf' => $this->data['csrf']
				)
			);
			$is_database  = FALSE;
		}
		if (strlen($password) < 4)
		{
			$this->result = array(
				'code'    => 'fail',
				'message' => '비밀번호는 4자 이상 입력해야합니다.',
				'data'    => array(
					'csrf' => $this->data['csrf']
				)
			);
			$is_database  = FALSE;
		}
		$password_confirm = $this->input->post('password_confirm', TRUE);
		if ($password !== $password_confirm)
		{
			$this->result = array(
				'code'    => 'fail',
				'message' => '비밀번호가 일치하지 않습니다.',
				'data'    => array(
					'csrf' => $this->data['csrf']
				)
			);
			$is_database  = FALSE;
		}
		$captcha = $this->input->post('captcha', TRUE);
		$row     = $this->captcha_model->captcha_check($captcha);
		if ($row->count == 0)
		{
			$this->result = array(
				'code'    => 'fail',
				'message' => '자동등록방지 코드가 맞지 않습니다.',
				'data'    => array(
					'csrf' => $this->data['csrf']
				)
			);
			$is_database  = FALSE;
		}
		$name      = $this->input->post('name', TRUE);
		$nick_name = $this->input->post('nick_name', TRUE);
		$email     = $this->input->post('email', TRUE);
		$url       = $this->input->post('url', TRUE);
		if ($is_database)
		{
			$r            = $this->member_model->sign_up($id, $password, $name, $nick_name, $email);
			$this->result = array(
				'code'    => $r['code'],
				'message' => $r['message'],
				'url'     => $url,
				'data'    => array(
					'csrf' => $this->data['csrf']
				)
			);
		}
		$this->output->set_content_type('application/json')
			->set_output(json_encode($this->result));
	}

 

반응형