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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<template>
<div class="mt-5">
<div class="row d-flex justify-content-center">
<div class="col-md-7">
<div class="card px-5 py-4">
<div class="form-data">
<div class="text-center mb-4">
<h4>Register</h4>
</div>
<div class="forms-inputs mb-4">
<span>First name</span>
<input type="text" v-model="firstname"
:class="{'form-control':true, 'is-invalid' : !validName(firstname) && firstnameBlured}"
@blur="firstnameBlured = true">
<div class="invalid-feedback">A valid first name is required!</div>
</div>
<div class="forms-inputs mb-4">
<span>Last name</span>
<input type="text" v-model="lastname"
:class="{'form-control':true, 'is-invalid' : !validName(lastname) && lastnameBlured}"
@blur="lastnameBlured = true">
<div class="invalid-feedback">A valid last name is required!</div>
</div>
<div class="forms-inputs mb-4">
<span>Email</span>
<input type="text" v-model="email"
:class="{'form-control':true, 'is-invalid' : !validEmail(email) && emailBlured}"
@blur="emailBlured = true">
<div class="invalid-feedback">A valid email is required!</div>
</div>
<div class="forms-inputs mb-4">
<span>Password</span>
<input type="password" v-model="password"
:class="{'form-control':true, 'is-invalid' : !validPassword(password) && passwordBlured}"
@blur="passwordBlured = true">
<div class="invalid-feedback">Password must be 8 character!</div>
</div>
<div class="forms-inputs mb-4">
<span>Confirm Password</span>
<input type="password" v-model="passwordConfirm"
:class="{'form-control':true, 'is-invalid' : (password !== passwordConfirm) && passwordConfirmBlured}"
@blur="passwordConfirmBlured = true">
<div class="invalid-feedback">Passwords must match!</div>
</div>
<div class="mb-3">
<button @click.stop.prevent="submit" class="btn btn-dark w-100">Register</button>
</div>
</div>
</div>
</div>
</div>
</div>
<Modal :title="modalTitle" v-if="showModal" @close="closeBtnAction">
<p v-text="modalText"></p>
</Modal>
</template>
<script>
import validator from 'validator';
import Modal from '@/components/Modal.vue';
export default {
name: 'Register',
components: {
Modal
},
data() {
return {
valid : false,
firstname: "",
firstnameBlured: false,
lastname: "",
lastnameBlured: false,
email : "",
emailBlured : false,
password:"",
passwordBlured:false,
passwordConfirm:"",
passwordConfirmBlured:false,
showModal: false,
modalTitle: '',
modalText: '',
closeBtnAction: null
}
},
methods: {
validate() {
this.firstnameBlured = true;
this.lastnameBlured = true;
this.emailBlured = true;
this.passwordBlured = true;
this.passwordConfirmBlured = true;
if (this.validName(this.firstname) && this.validName(this.lastname)
&& this.validEmail(this.email) && this.validPassword(this.password)
&& this.password === this.passwordConfirm) {
this.valid = true;
}
else {
this.valid = false;
}
},
validEmail: (email) => validator.isEmail(email),
//validPassword: (password) => validator.isStrongPassword(password, { minLength: 8 }),
validPassword: (password) => password.length > 7,
validName: (name) => validator.isAlpha(name) && name.length,
submit() {
this.validate();
const successMsg = () => {
this.modalTitle = 'Success!';
this.modalText = 'You successfully registered!';
this.showModal = true;
this.closeBtnAction = () => this.$router.push('/');
};
const failureMsg = () => {
this.modalTitle = 'Failure!';
this.modalText = 'You failed to register.';
this.showModal = true;
this.closeBtnAction = () => this.showModal = false;
};
if (this.valid) {
const requestData = {
firstname: this.firstname,
lastname: this.lastname,
email: this.email,
password: this.password,
confirmPassword: this.passwordConfirm,
};
this.$store.dispatch('register', requestData)
.then(() => successMsg())
.catch(() => failureMsg());
}
else {
failureMsg();
}
}
}
}
</script>
<style lang="scss">
.forms-inputs input:focus {
border: 2px solid #000
}
</style>
|