It could be the battery or the charger. It could be something internal as well, but try sticking with the first two for now.
First, try charging it when it is off. Turn the laptop off, not hibernate or sleep but off. Then try charging it. If it charges without a problem, but then still has issues when the laptop is on and you try to charge it, you may need to replace the charger. That is a typical sign of it going bad.
If the problem happens when the laptop is turned both on or off, then test the charger to see if the connection going into the laptop starts and stops if you wiggle the cord. If it does, again, you probably need to replace the charger. If it does, but it isn't just the cord that is moving, the internal port is moving as well, then you may have to have that replaced. If it is loose.
Lastly, if none of the above applies, then I would be looking at a new battery.