There's no problem taking the foam in your carry-on. I've done it many, many times.
The restriction is for liquids/foams that are individually 3 ounces or more. A can of Rogaine Foam is 2.11 ounces. I routinely pack the foam, travel toothpaste, small can of shaving cream and deodorant with zero problems. You just have to put everything in a quart-size ziplock and send it through the X-ray by itself.