@@ -9,6 +9,22 @@ document.addEventListener("DOMContentLoaded", function () {
99 const otherOrganisationDiv = document . getElementById ( "other_organisation_div" ) ;
1010 const otherOrganisationInput = document . getElementById ( "organisation_other" ) ;
1111
12+ const form = document . getElementById ( "registerForm" ) || document . getElementById ( "editProfileForm" ) ;
13+ if ( ! form ) {
14+ return ;
15+ }
16+
17+ function validateOtherOrganisation ( ) {
18+ const otherOrganisation = otherOrganisationInput . value . trim ( ) ;
19+ if (
20+ organisationDropdown . value === "Other" &&
21+ ( ! otherOrganisation || otherOrganisation . length < 3 )
22+ ) {
23+ otherOrganisationInput . setCustomValidity ( "Please specify your organisation (at least 3 characters)." ) ;
24+ } else {
25+ otherOrganisationInput . setCustomValidity ( "" ) ;
26+ }
27+ }
1228
1329 organisationDropdown . addEventListener ( "change" , function ( ) {
1430 if ( this . value === "Other" ) {
@@ -17,9 +33,23 @@ document.addEventListener("DOMContentLoaded", function () {
1733 } else {
1834 otherOrganisationDiv . style . display = "none" ;
1935 otherOrganisationInput . required = false ;
20- otherOrganisationInput . value = "" ; // Clears the value if hidden
36+ otherOrganisationInput . value = "" ;
37+ otherOrganisationInput . setCustomValidity ( "" ) ;
2138 }
2239 } ) ;
40+
41+ otherOrganisationInput . addEventListener ( "input" , validateOtherOrganisation ) ;
42+
43+ // Validation for "Other" organisation input on form submission will prevent form submission
44+ form . addEventListener ( "submit" , function ( event ) {
45+ validateOtherOrganisation ( ) ;
46+
47+ if ( ! form . checkValidity ( ) ) {
48+ form . reportValidity ( ) ;
49+ event . preventDefault ( ) ;
50+ }
51+ } ) ;
52+
2353 // Run on page load to ensure form is in correct state if form submission fails
2454 // and page/form is re-rendered with previous values.
2555 // e.g. password don't match etc...
0 commit comments