빈칸 체크 함수
<script>
function q1() {
let value = $('#input-q1').val();
console.log(value)
if (value == '') {
alert('입력하세요!')
} else {
alert(value)
}
}
</script>
이메일 판별 함수
* 문자열 포함 체크
<script>
function q2() {
let email = $('#input-q2').val();
console.log(email.includes('@'))
if (email.includes('@')) {
let domainWithDot = email.split('@')[1];
let onlyDomain = domainWithDot.split('.')[0];
alert(onlyDomain);
} else {
alert('이메일이 아닙니다.');
}
}
</script>
* 추출 방법(콘솔)
let txt = abc@abc.com
txt.split('@') // abc, abc.com으로 나뉨
txt.split('@')[1] // 첫번째 선택으로 abc.com만 나타남
txt.split('@')[1].split('.') // abc, com으로 나뉨
txt.split('@')[1].split('.')[0] // abc만 나타남
// if 밑에 'let domain = txt.split('@')[1].split('.')[0]' 작성해도 추출됨
HTML 붙이기/지우기
<script>
function q3() {
let newName = $('#input-q3').val();
if (newName == '') {
alert('이름을 입력하세요');
return;
}
* temp_html 사용시 꼭 `(백팁) 사용, 백팁 사용하지 않으면 적용이 안됨
let temp_html = `<li>${newName}</li>`;
console.log(temp_html)
$('#names-q3').append(temp_html);
}
function q3_remove() {
$('#names-q3').empty();
}
</script>