-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlingSbumitandValidation.html
112 lines (104 loc) · 3.56 KB
/
handlingSbumitandValidation.html
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Handling Submit & Form Validation</title>
<style>
form {
border: 1px solid #ddd;
padding:5px;
width:225px;
background: #efefef;
}
label {
display: block;
margin-top: 5px;
}
input, textarea, select, option{
min-width: 200px;
}
</style>
</head>
<body>
<div id="app">
<!-- @submit yaitu v-on:submit -->
<form @submit="submitForm($event)" action="http://example.com/add-product" method="post">
<label>Title:</label>
<input type="text" v-model="title">
<label>Description</label>
<textarea v-model="description"></textarea>
<label>Authors:</label>
<input type="text" v-model="authors">
<label>Price:</label>
<input type="text" v-model="price">
<label>Categories:</label>
<select v-model="categories" multiple>
<option v-for="option in options" value="option.value">
{{ option.text }}
</option>
</select>
<label></label>
<input type="submit" value="submit">
</form>
</div>
<script src="lib/vue.js"></script>
<script>
new Vue({
el: '#app',
data: {
title: '',
description: '',
authors: '',
price: 0,
categories: [],
options: [
{text: 'Graphic Programming', value: '01'},
{text: 'Mobile Application Development', value: '02'},
{text: 'Virtual and Augmented Reality', value: '03'}
]
},
methods: {
submitForm(event){
let error = 0;
this.error = []
if (this.title.length < 3){
error++
// menambahkan set focus
this.$refs.title.focus()
alert('Title Minimal 3 karakter!')
}
else if (this.description.length > 5000){
error++
this.error.push('Descrption Maximal 5000 karakter');
}
else if (this.authors.length < 3){
error++
alert('authors minimal 3 karakter')
}
else if(this.price < 0){
error++
alert('Price tidak boleh minus!')
}
else if(this.categories.length === 0){
error++
alert('Pilih minimal 1 category!')
}
if( error === 0 ){
alert('Terima kasih telah mengisi data dengan benar!')
// kirim data ke server
}
//console.log(event);
//kode validasi
//kode kirim ke server
// kode status informasi
//alert('terima kasih');
// block redirect ke action
event.preventDefault()
}
}
});
</script>
</body>
</html>