ANSWER: To consider the age and knowing when the bad behavior warrants discipline with a spanking, turn to the Old Testament book of Proverbs where wise King Solomon gives us principles to follow in our everyday life. He states "A refusal to correct is a refusal to love; love your children by disciplining them" (Proverbs 13:24, HBFV).
The Bible definitely puts the responsibility on you as the parent. As someone who loves their child, you probably know their attitude more than a stranger. Allowing the child to have the control with their bad behavior is not showing them love and sometimes spanking is needed.
Be sure to convey that you love them before the spanking so they will know you are not correcting them in anger but in love. This may take a little practice. Sometimes you may have to count to 10 before giving correction so this act so it will not be in anger. Then again, if the spanking is done promptly, as is mentioned in scripture, you may not need to do the counting.
Prompt discipline is important as a young child has a very short memory. Also, you may want to say a short prayer to ask God for the wisdom and self-control so that the spanking (discipline) is done in love.
In regard to your question concerning at what age spanking is appropriate, Proverbs 19 says we are to disciple our children when they are at the age young enough to learn. It states we should, "Chasten your son while there is hope, but do not set your heart on his destruction" (Proverbs 19:18). No actual age is mentioned, leaving that up to the parent's discretion.
It is, however, vital that you instill attitudes of respect into your child, before they reach school age, for those who will be in authority over them. This will prepare them to have a proper attitude as they grow up (Proverbs 29:17).
Discipline also helps instill in a child the willingness to accept the guidance of a parent and not run wild. When they obey they will be happy. The most important thing for a child to learn when he or she is very young is that they are loved. The second most important principle for a child to learn is that they need to obey their parents.
Consider the admonition to adults in the New Testament book of Hebrews which states that God treats us just like his children (Hebrews 12:5). The Bible states we are corrected because, like a good parent, the Eternal loves us (Hebrews 12:6).
As the child grows into an adult they will always need to answer to someone, so it would be good to learn this important principle early on. In regard to our (or any) society that considers spanking (disciplining) a form of child abuse, we must be discrete when doing so in public. Our nation has gone so far away from God and His laws that they think ANY kind of correction is abuse.
Many leaders and citizens alike have listened to the erroneous teachings of Benjamin Spock, MD (1903 - 1998) who advocated very little spanking and was branded as the "father of permissiveness." It would be better not to correct in public but to wait until you get home or to a private place.
One thing you could do would be to give the child something to work for. For instance, offer a reward to be given when you return home for good behavior while you are out. If there is an infraction then no reward is given.
The Bible is not against spanking as a form of correction so long as it is done in love and in a judicious manner. Whatever you do, NEVER allow the child to go on being continually disrespectful and having the control in all situations. Take your child-rearing concerns in prayer to God the Father, our perfect parent. Then be patient and do the best you can, always seeking God's advice and the guidance he offers in Scripture.