-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathform.js
More file actions
44 lines (37 loc) · 1.3 KB
/
form.js
File metadata and controls
44 lines (37 loc) · 1.3 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
document.addEventListener("DOMContentLoaded",()=>{
let form = document.forms[0]; //DOM nya form
form.addEventListener("submit",(event)=>{
event.preventDefault();//mencegah untuk submit
let datanyaform = new FormData(form);// object dari FormData
let isbenar = true;
datanyaform.forEach((value,key) => {
if(value=="" || value==0){
console.log(form[key]);
form[key].style.backgroundColor="pink";
console.log(key+" tidak boleh kosong");
isbenar = false;
}else{
isbenar = true;
}
});
console.log([...datanyaform.entries()]);
console.log(isbenar);
if(isbenar){
if(confirm("Apakah anda yakin?")){
form.submit();
}
return false;
}else{
alert("tidak boleh kosong");
}
});
form.querySelectorAll('input,select').forEach((item)=>{
item.addEventListener("change",(event)=>{
if(item.value==''||item.value==0){
item.style.backgroundColor="pink";
}else{
item.style.backgroundColor="white";
}
})
});
})