@@ -125,49 +125,77 @@ function preLoader() {
125
125
preloader . style . display = "none" ;
126
126
}
127
127
128
+ /*==================== END EMAIL VIA emailJs ====================*/
129
+ const sendEmail = ( params ) => {
130
+ const serviceID = "service_embce24" ;
131
+ const templateID = "template_cci0niw" ;
132
+
133
+ emailjs
134
+ . send ( serviceID , templateID , params )
135
+ . then ( ( res ) => {
136
+ document . getElementById ( "name" ) . value = "" ;
137
+ document . getElementById ( "email" ) . value = "" ;
138
+ document . getElementById ( "message" ) . value = "" ;
139
+
140
+ document . getElementById ( "email-submit" ) . innerHTML = `
141
+ Send message
142
+ <i class="uil uil-message button__icon"></i>` ;
143
+
144
+ alert ( "Your message send susscessfully!" ) ;
145
+ return ;
146
+ } )
147
+ . catch ( ( err ) => {
148
+ document . getElementById ( "email-submit" ) . innerHTML = `
149
+ Send message
150
+ <i class="uil uil-message button__icon"></i>` ;
151
+ console . error ( err ) ;
152
+ } ) ;
153
+ } ;
154
+
155
+ /*==================== EMAIL VALIDATION API ====================*/
156
+ const validateEmail = ( email , params ) => {
157
+ const apiKey = "cf7383198f5a4c8a8b282a00c50dd08b" ;
158
+ const apiUrl = `https://emailvalidation.abstractapi.com/v1/?api_key=${ apiKey } &email=${ encodeURIComponent (
159
+ email
160
+ ) } `;
161
+
162
+ fetch ( apiUrl )
163
+ . then ( ( response ) => response . json ( ) )
164
+ . then ( ( data ) => {
165
+ if ( data . deliverability == "DELIVERABLE" ) sendEmail ( params ) ;
166
+ else {
167
+ alert ( "Invalid email address" ) ;
168
+ document . getElementById ( "email-submit" ) . innerHTML = `
169
+ Send message
170
+ <i class="uil uil-message button__icon"></i>` ;
171
+ }
172
+ } )
173
+ . catch ( ( error ) => {
174
+ console . error ( error ) ;
175
+ sendEmail ( params ) ;
176
+ } ) ;
177
+ } ;
178
+
128
179
/*==================== FORM SUBMIT ====================*/
129
180
document . getElementById ( "contact-form" ) . addEventListener ( "submit" , ( e ) => {
130
181
e . preventDefault ( ) ;
131
182
132
- //button animation
133
- document . getElementById ( "email-submit" ) . innerText = "Sending..." ;
183
+ const name = document . getElementById ( "name" ) . value ;
184
+ const email = document . getElementById ( "email" ) . value ;
185
+ const message = document . getElementById ( "message" ) . value ;
134
186
135
- if (
136
- document . getElementById ( "name" ) . value != "" &&
137
- document . getElementById ( "email" ) . value != "" &&
138
- document . getElementById ( "message" ) . value != ""
139
- ) {
140
- let params = {
141
- name : document . getElementById ( "name" ) . value ,
142
- email : document . getElementById ( "email" ) . value ,
143
- message : document . getElementById ( "message" ) . value ,
144
- } ;
145
-
146
- const serviceID = "service_embce24" ;
147
- const templateID = "template_cci0niw" ;
148
-
149
- emailjs
150
- . send ( serviceID , templateID , params )
151
- . then ( ( res ) => {
152
- document . getElementById ( "name" ) . value = "" ;
153
- document . getElementById ( "email" ) . value = "" ;
154
- document . getElementById ( "message" ) . value = "" ;
187
+ const emailRegex = / ^ [ a - z A - Z 0 - 9 . _ - ] + @ [ a - z A - Z 0 - 9 . - ] + \. [ a - z A - Z ] { 2 , 4 } $ / ;
188
+ const isEmail = emailRegex . test ( email ) ;
155
189
156
- document . getElementById ( "email-submit" ) . innerHTML = `
157
- Send message
158
- <i class="uil uil-message button__icon"></i>` ;
159
-
160
- alert ( "Your message send susscessfully!" ) ;
161
- } )
162
- . catch ( ( err ) => {
163
- document . getElementById ( "email-submit" ) . innerHTML = `
164
- Send message
165
- <i class="uil uil-message button__icon"></i>` ;
166
- console . log ( err ) ;
167
- } ) ;
168
- } else {
169
- alert ( "Please fill out all the fields" ) ;
190
+ //validate email
191
+ if ( ! isEmail ) {
192
+ alert ( "Invalid email address" ) ;
193
+ return 0 ;
170
194
}
195
+
196
+ const params = { name : name , email : email , message : message } ;
197
+ document . getElementById ( "email-submit" ) . innerText = "Sending..." ;
198
+ validateEmail ( email , params ) ;
171
199
} ) ;
172
200
173
201
/*==================== GITHUB CALENDAR ====================*/
0 commit comments