Setting terms for breaking a contract is not illegal. You have to give that amount of notice and also pay them the fine.
Notice period:
worked there 3 months to 1 year = 10 days
1 year to 3 years = 20 days
3 years and above = 30 days
They can’t deduct it from your pay, and they can’t withhold your pay, but they can sue you for the amount you agreed to in the contract. They can also sue for damages that the company sustains and get you blacklisted if you just disappear with no notice.
If you sign a contract as a responsible adult, it means you’re making a promise that you’re legally bound to.
If you’re unable to assume that responsibility, don’t sign contracts!