728x90

1. 자바스크립트 빈 문자열 검사 함수

function isEmpty(strIn)
{
    if (strIn === undefined)
    {
        return true;
    }
    else if(strIn == null)
    {
        return true;
    }
    else if(strIn == "")
    {
        return true;
    }
    else
    {
        return false;
    }
}

* 출처 : https://stackoverflow.com/questions/19592721/isempty-is-not-defined-error

 

isEmpty is not defined-error

I was getting an error as isEmpty is not defined .what i have to do .while alerting too it is not showing any alert. <!DOCTYPE html> <html> <head> ...

stackoverflow.com

2. 활용

        function insertComment() {
            var boardId = $('input[name=boardId]').val()
            var content = document.getElementById("content").value;

            if (isEmpty(content) == true) {
                alert('댓글을 입력해주세요.')
                return false;
            } else {
                $.ajax({
                    type: 'POST',
                    url: '/board/comment/write',
                    data: {
                        boardId: boardId,
                        content: content
                    },
                    success: function (response) {
                        getComments()
                        console.log("insert success")
                    },
                    error: function (response) {
                        console.log("insertComment error : " + response)
                    },
                })
            }
        }
  • 댓글 추가 ajax 코드
  • 댓글 내용 없이 작성버튼을 클릭하면 경고문을 띄우고 리턴.

+ Recent posts