Shaving the beard in whole or in part is a sin which is not permissible to do. The prayer of one who commits a sin such as shaving the beard and the like is valid if he does it as prescribed by Allaah, according to scholarly consensus. The prayers of those who pray behind him, if he leads the prayer, are also valid, according to the more correct of the two scholarly opinions.
Tuhfat al-Ikhwaan, p. 114, by Shaykh ‘Abd al-‘Azeez ibn Baaz.
The fact that his leading of the prayer is valid is indicated by the fact that the Sahaabah (may Allaah be pleased with them) prayed behind unjust leaders and ruler. ‘Abd-Allaah ibn ‘Umar and Anas (may Allaah be pleased with him) prayed behind al-Hajjaaj al-Thaqafi. Al-Shaafa’i said: And he – meaning al-Hajjaaj – was bad enough (as an evildoer).
Once this is established, there is no point in saying, “I will not pray behind this imam.” Rather you should pray behind him if not doing so will lead to evil consequences, division or conflict, because conflict is worse.
Shaykh Ibn Baaz (may Allaah have mercy on him) said: If by not praying behind him the worshipper will miss out on Jumu’ah and prayers in congregation, then no one does that except an innovator who is differing from the Sahaabah (may Allaah be pleased with them). Similarly, if the imam has been appointed by those in authority and there is no shar’i purpose to be served by not praying behind him, then you should not refuse to pray behind him, rather it is better to pray behind him.
Majmoo’ Fataawa al-Shaykh Ibn Baaz, 9/388.
Al-Nawawi (may Allaah have mercy on him) said – concerning one who leads people in prayer when they do not like him: Our companions said: His leading the prayer is makrooh if they dislike him because of something that is condemned in sharee’ah, such as an oppressive ruler or one who attains the position of imam by force and does not deserve it, or one who does not take precautions to avoid naajis (impure) things… If no such thing applies then it is not makrooh. … Then he said: When we say that it is makrooh, that is limited to the imam; as for the members of the congregation who do not like him, it is not makrooh for them to pray behind him.
With regard to replacing him, if you are able to replace him with another imam who is more religiously committed and doing so will not result in a greater evil, then it is better to do that, otherwise it is better to leave him.
The evidence that an imam who commits sin openly should not be allowed to lead the prayers is to be found in the report narrated by Abu Dawood (481), which says that a man led some people in prayer and he spat in the qiblah (of the mosque) when the Messenger of Allaah (peace and blessings of Allaah be upon him) was looking on. The Messenger of Allaah (peace and blessings of Allaah be upon him) said, when he had finished praying: “He should not lead you in prayer.” After that he wanted to lead them in prayer again, but they did not let him, and they told him what the Messenger of Allaah (peace and blessings of Allaah be upon him) had said. He mentioned that to the Messenger of Allaah (peace and blessings of Allaah be upon him) and he said: “Yes.” And I (the narrator) think that he said: “You have offended Allaah and His Messenger.” Classed as hasan by al-Albaani in Saheeh Abi Dawood.
This is so long as preventing him from leading the prayers will not lead to a greater evil; if that is the case, then you should pray behind him, and it is not permissible to refrain from praying in congregation because of the evildoing of the imam, as stated above.
Shaykh Ibn Baaz (may Allaah have mercy on him) said: If it is possible for a person to prevent a person who commits evil openly from leading the prayer, then he has to do that; but if he has been appointed by someone else and it is not possible to stop him leading the prayers or it is only possible to stop him by means of a greater evil that is more harmful than the evil of the imam who commits evil openly, then it is not permissible to ward off a lesser evil by means of a greater one. Islam came to achieve and complete what is in people’s best interests and to wipe out and reduce evil as much as possible.
Majmoo’ Fataawa al-Shaykh Ibn Baaz, 9/377
See also question no. 13465
And Allaah knows best.