@@ -10,8 +10,11 @@ function brandChanged() {
1010 if ( style === 'block' ) {
1111 tb . value = '' ;
1212 tb . required = true ;
13+ tb . setCustomValidity ( "" ) ;
1314 tb . focus ( ) ;
1415 } else {
16+ tb . value = "" ;
17+ tb . setCustomValidity ( "" ) ;
1518 tb . required = false ;
1619 }
1720}
@@ -27,8 +30,11 @@ function categoryChanged() {
2730 if ( style === 'block' ) {
2831 tb . value = '' ;
2932 tb . required = true ;
33+ tb . setCustomValidity ( "" ) ;
3034 tb . focus ( ) ;
3135 } else {
36+ tb . value = "" ;
37+ tb . setCustomValidity ( "" ) ;
3238 tb . required = false ;
3339 }
3440}
@@ -44,8 +50,41 @@ function topicChanged() {
4450 if ( style === 'block' ) {
4551 tb . value = '' ;
4652 tb . required = true ;
53+ tb . setCustomValidity ( "" ) ;
4754 tb . focus ( ) ;
4855 } else {
56+ tb . value = "" ;
57+ tb . setCustomValidity ( "" ) ;
4958 tb . required = false ;
5059 }
5160}
61+ const form = document . querySelector ( "form" ) as HTMLFormElement ;
62+ const bfield = < HTMLInputElement > document . getElementById ( 'brand-field' ) ;
63+ const cfield = < HTMLInputElement > document . getElementById ( 'category-field' ) ;
64+ const tfield = < HTMLInputElement > document . getElementById ( 'topic-field' ) ;
65+ function wireClearOnInput ( input : HTMLInputElement ) {
66+ input . addEventListener ( "input" , ( ) => {
67+ input . setCustomValidity ( "" ) ;
68+ } ) ;
69+ }
70+
71+ if ( bfield ) wireClearOnInput ( bfield ) ;
72+ if ( cfield ) wireClearOnInput ( cfield ) ;
73+ if ( tfield ) wireClearOnInput ( tfield ) ;
74+
75+ form . addEventListener ( "submit" , ( e : Event ) => {
76+ [
77+ { el : bfield , msg : "Please enter a valid brand." } ,
78+ { el : cfield , msg : "Please enter a valid category." } ,
79+ { el : tfield , msg : "Please enter a valid topic." }
80+ ] . forEach ( f => {
81+ if ( f . el . required && ! f . el . value . trim ( ) ) {
82+ e . preventDefault ( ) ;
83+ f . el . setCustomValidity ( f . msg ) ;
84+ f . el . reportValidity ( ) ;
85+ f . el . focus ( ) ;
86+ } else {
87+ f . el . setCustomValidity ( "" ) ;
88+ }
89+ } ) ;
90+ } ) ;
0 commit comments