@@ -7,6 +7,50 @@ class FilesHTMLElement extends HTMLElement {
77 selectedProject = undefined ;
88 autostartProject = undefined ;
99
10+ /**
11+ * Validates project name and returns error message or empty string if valid.
12+ * Rules: not empty, no leading/trailing spaces, only alphanumeric and spaces allowed.
13+ */
14+ validateProjectName ( name ) {
15+ if ( ! name || name . length === 0 ) {
16+ return 'Project name cannot be empty' ;
17+ }
18+ if ( name . startsWith ( ' ' ) ) {
19+ return 'Project name cannot start with a space' ;
20+ }
21+ if ( name . endsWith ( ' ' ) ) {
22+ return 'Project name cannot end with a space' ;
23+ }
24+ if ( ! / ^ [ a - z A - Z 0 - 9 ] + $ / . test ( name ) ) {
25+ return 'Only letters, numbers, and spaces are allowed' ;
26+ }
27+ return '' ;
28+ }
29+
30+ /**
31+ * Updates the validation UI based on input value.
32+ */
33+ updateValidationUI ( ) {
34+ const input = this . shadowRoot . getElementById ( 'newProjectName' ) ;
35+ const hint = this . shadowRoot . getElementById ( 'validationHint' ) ;
36+ const createBtn = this . shadowRoot . getElementById ( 'createbutton' ) ;
37+ const value = input . value ;
38+
39+ const error = this . validateProjectName ( value ) ;
40+
41+ if ( error ) {
42+ input . classList . add ( 'invalid' ) ;
43+ hint . textContent = error ;
44+ hint . classList . add ( 'visible' ) ;
45+ createBtn . setAttribute ( 'disabled' , '' ) ;
46+ } else {
47+ input . classList . remove ( 'invalid' ) ;
48+ hint . textContent = '' ;
49+ hint . classList . remove ( 'visible' ) ;
50+ createBtn . removeAttribute ( 'disabled' ) ;
51+ }
52+ }
53+
1054 connectedCallback ( ) {
1155 const shadow = this . attachShadow ( { mode : 'open' } )
1256
@@ -77,9 +121,16 @@ class FilesHTMLElement extends HTMLElement {
77121 this . openProject ( this . selectedProject . name ) ;
78122 } ) ;
79123
124+ const newProjectInput = this . shadowRoot . getElementById ( "newProjectName" ) ;
125+ newProjectInput . addEventListener ( "input" , ( ) => {
126+ this . updateValidationUI ( ) ;
127+ } ) ;
128+
80129 this . shadowRoot . getElementById ( "createbutton" ) . addEventListener ( "click" , ( event ) => {
81- var project = this . shadowRoot . getElementById ( "newProjectName" ) . value ;
82- this . createProject ( project ) ;
130+ var project = newProjectInput . value ;
131+ if ( ! this . validateProjectName ( project ) ) {
132+ this . createProject ( project ) ;
133+ }
83134 } ) ;
84135
85136 this . setAutoStartProject ( this . autostartProject ) ;
0 commit comments