I have a quadratic program
$$\text{minimize} \quad \sum_{i=1}^m (u_i^2 - 2Mu_i + 2M |a_i^T x - b_i|)$$ $$\text{subject to} \quad 0 \le u_i \le \min \{ M , |a_i^T x - b_i| \}$$
Now it says the optimal choice of $u_i$ is $|a_i^Tx -b_i|$ if $|a_i^Tx - b_i| \le M$. If $|a_i^T x - b_i| > M$, optimal choice of $u_i = M$. I don't see how that is the case. I tried completing the square and got $$\sum_{i=1}^m (u_i - M)^2 - (M - |a_i^Tx - b_i|)^2 + |a_i^Tx - b_i|^2$$ but it is unclear how I can get optimal points.
Your reformulation shows that you want $u_i$ to be as close to $M$ as possible. If $M \le |a_i^T x-b_i|$, then $u_i=M$ is feasible and clearly optimal. If $M > |a_i^T x-b_i|$, then $u_i=M$ is not feasible, but $u_i=|a_i^T x-b_i|$ is feasible and taking $u_i<|a_i^T x-b_i|$ would yield a worse objective value.