How can I manage custom size and size variant selections in Shopify?

I’m working on a Shopify store where I have both size variants and a custom size input for products. I need to implement logic so that:

  1. If a user selects a size variant, the custom size input should not be included in the cart.
  2. Conversely, if the user inputs a custom size, the selected size variant should be excluded from the cart.

This is a part of my liquid code:


      
      {% if product.tags contains 'Custom Size' %}
        

          
          

            Or
            
          

          
              
            
            
            

              Selected size: 0 x 0 Cm
            

            
              

                
                
              

              
                
                
              

              
                

Cm

        
              

            

          

        

and this is the script code:


I have try to intercept the form after submission but this does not works.