As long as the standard is across the board they can do it. Ask Disney, they've gotten sued for dress codes over all sorts of stuff, small stud earrings ok for women, not for men, women must wear bras, men don't - and Disney wins every time (of course Disney has a legal staff the size of the Matterhorn too). Many places allow men and women to wear different styles of shoes, women can wear slacks or skirts, men can only wear slacks, and on and on.
The reality is that if company XYZ does not like guys with long hair (policy or not) they will not hire you if your hair is long - and since they have so many applications for the job, its going to be hard to do anything about it.