Basis for legal enforcement:
Hijab is obligatory. It has been commanded in the Quran and ahadith, and there is Ijma' on its obligation.
Refraining from an obligation is a sin. Sins can be stopped by force under the umbrella of Nahi 'an al-Munkar (forbidding wrong) and Ta'zir. This includes every sin since there is no text which differentiates between one sin and another in this regard.
The means of stopping wrongdoing include advise, censure, threat and eventually more severe Ta'zir punishments like imprisonment or flogging depending on severity of the sin and persistence of the sinner etc. And some madhabs also allow for Ta'zir in the form of financial penalties but this is disputed.
أجمع العلماء على أن التعزير مشروع في كل معصية لا حد فيها ولا كفارة والمعاصي فرعان: ترك واجب وفعل محرم
The scholars have a consensus that Ta'zir is prescribed for every sin for which there is no Hadd or Kaffarah. And sins include: leaving an obligation or doing a haram act
— Majmoo‘ al-Fataawa - Ibn Taymiyya
There are several verses and ahadith which form the basis for this, I will mention only two:
الذين إن مكناهم في الأرض أقاموا الصلاة وآتوا الزكاة وأمروا بالمعروف ونهوا عن المنكر
Those who, if We give them authority in the land, establish prayer and give zakah and enjoin what is right and forbid what is wrong.
— Quran 22:41
من رأى منكم منكرا فليغيره بيده
Whoever among you sees an evil action, let him change it with his hand
Examples of enforcement in the views of pre-modern scholars:
وتمنع المرأة الشابة من كشف الوجه بين الرجال لا لانه عورة بل لخوف الفتنة
Young women will be stopped from uncovering their faces in the presence of men, not because it is 'awrah but because of the fear of fitnah
— Durr al-Mukhtaar- al-Haskafi
When this applies to veiling the face then logically it applies more strictly to the minimal hijab which covers the 'awrah and is obligatory by consensus.
ويجب عليه منع النساء من الخروج متزينات متجملات، ومنعهن من الثياب التي يكن بها كاسيات عاريات، كالثياب الواسعة والرقاق، ... وإن رأى ولي الأمر أن يفسد على المرأة - إذا تجملت وتزينت وخرجت - ثيابها بحبر ونحوه، فقد رخص في ذلك بعض الفقهاء وأصاب، وهذا من أدنى عقوبتهن المالية.وله أن يحبس المرأة إذا أكثرت الخروج من منزلها، ولا سيما إذا خرجت متجملة
It is obligatory on the ruler to stop women from going out while adorned and beautified. And to stop them from (going out when) wearing clothes in which they seem naked such as clothing made from a thin fabric ... If the ruler considers it appropriate he can ruin such clothes by pouring ink etc. over them, some jurists have considered this permissible and correct, and this will be considered as a minor financial penalty. And he may imprison a woman who goes out too often from her home (without a valid need), especially if she who goes out while adorned
— Al-Turuq al-Hukamiyyah - Ibn al-Qayyim
Exposing adornments means not having proper hijab. And when these things can be corrected then so can more severe lapses in hijab.