@@ -5,22 +5,38 @@ import { useState } from "react";
55export default function Letter ( ) {
66 const [ email , setEmail ] = useState ( "" ) ;
77 const [ subscriptionSuccess , setSubscriptionSuccess ] = useState ( false ) ;
8+ const [ error , seterror ] = useState ( "" ) ;
9+
10+ function validEmail ( email ) {
11+ let re =
12+ / ^ ( ( [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ( \. [ ^ < > ( ) \[ \] \\ . , ; : \s @ " ] + ) * ) | ( " .+ " ) ) @ ( ( \[ [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } \. [ 0 - 9 ] { 1 , 3 } ] ) | ( ( [ a - z A - Z \- 0 - 9 ] + \. ) + [ a - z A - Z ] { 2 , } ) ) $ / ;
13+ if ( re . test ( email ) ) return true ;
14+ else return false ;
15+ }
816
917 const handleSubmit = ( e ) => {
1018 e . preventDefault ( ) ;
1119
12- // Make the POST request using Axios
13- axios
14- . post ( "/api/subscribes" , { email } )
15- . then ( ( response ) => {
16- // Handle the success response here
17- console . log ( response . data ) ;
18- setSubscriptionSuccess ( true ) ;
19- } )
20- . catch ( ( error ) => {
21- // Handle the error here
22- console . error ( error ) ;
23- } ) ;
20+
21+ if ( email === "" ) {
22+ seterror ( "**E-mail is Required!" ) ;
23+ } else if ( ! validEmail ( email ) ) {
24+ seterror ( "**Enter a valid E-mail!" ) ;
25+ }
26+ else {
27+ // Make the POST request using Axios
28+ axios
29+ . post ( "/api/subscribes" , { email } )
30+ . then ( ( response ) => {
31+ // Handle the success response here
32+ console . log ( response . data ) ;
33+ setSubscriptionSuccess ( true ) ;
34+ } )
35+ . catch ( ( error ) => {
36+ // Handle the error here
37+ console . error ( error ) ;
38+ } ) ;
39+ }
2440 } ;
2541
2642 const handleEmailChange = ( e ) => {
@@ -91,12 +107,20 @@ export default function Letter() {
91107 < button
92108 type = "submit"
93109 className = "py-3 px-5 w-full text-sm font-medium text-center text-white rounded-lg border cursor-pointer bg-primary-700 border-primary-600 sm:rounded-none sm:rounded-r-lg hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800 bg-gradient-to-r from-indigo-800 to-purple-500"
110+ onClick = { handleSubmit }
94111 >
95112 Subscribe
96113 </ button >
97114 </ div >
98115 </ div >
99116 </ form >
117+ { error === "**E-mail is Required!" && (
118+ < small className = "text-lg font-medium text-red-400" > **E-mail is Required!</ small >
119+ ) }
120+
121+ { error === "**Enter a valid E-mail!" && (
122+ < small className = "text-lg font-medium text-red-400" > **Enter a valid E-mail!</ small >
123+ ) }
100124 </ >
101125 ) }
102126 </ div >
0 commit comments