In general terms, yes you can – but a dress code must sit within the context of discrimination legislation and must be applied in an even-handed way. There are two cases that illustrate how this works.
In 2016 receptionist Nicola Thorp forced UK outsourcing firm Portico to change its dress code policy after she was sent home from working at PwC for refusing to wear high heels. Her role was to escort people to and from meetings all day, and she felt unable to do that in 2-4 inch heels as demanded – but more importantly, she pointed out that a man in the same role would not have been subject to the same requirement.
A recent Jersey case also involved a receptionist, Eleanor Mahoney, whose discrimination and unfair dismissal claims against JTC Group were struck out in October 2017. During her probationary period, her employer told her that she was not always dressed sufficiently smartly. The claimant said that she was being discriminated against for being a "plus-sized woman." She did not complain that male colleagues were treated differently. The Discrimination (Jersey) Law 2013 does not consider "size" a protected characteristic in the same way as sex or age, and her claims failed.
In summary, employers can enforce dress codes, but only within the confines of the discrimination law. The Portico requirement for a female receptionist to wear 2-4 inch heels was not legitimate when no equivalent requirement was placed on men. Employers who want to enforce a dress code should consider the discrimination law and whether their proposals meet it. Policies should be clear, fair and evenly applied. The reputational damage in getting this wrong can be significant.