A lot of jeans have zippers that open on their own. For me, it is quite uncomfortable to be re-zipping them in the middle of the street.
What can I do to prevent this?
In-built lock. Its already there on your zip. Just check it.
Most of the jeans zips now come with In-built lock. When you zip just make sure that zip-hooker is placed vertically Down (this will lock the zip.) Just try to open the zip with hook placed down. It won't open Just like below image.(Zip hook placed vertically down here.)
If you zip and don't keep hooker down zip will eventually run down due to strain on it.
Visit YKK website for more info. If you see zips anywhere, there's 70% chance that it's made by YKK.
Some low quality jeans might not use YKK produced Zip. That's why you should always check YKK mark on zip before buying jeans.
Two solutions, with some top notch drawing (I'm clearly the next Vincent Van Gogh).
Tie a thin piece of string through your zipper the hole at the top of your zipper and your belt. However, have the string go through the inside of your pants, then out from the top of your pants, and then tied into your belt. This will be much less visible than if the string went outside the pants, especially if you can find string the same colour as your belt.
Put tape on the inside of your pants, where it's not visible. Use this tape to tape the zipper to the top of your pants.
You could avoid the need to constantly rezip jeans by sidestepping the problem entirely.
I only wear button/rivet fly jeans for this exact reason.
All major manufacturers of jeans provide designs without zips, if you find unzipping is a constant worry, they're a very simple solution.
Shevliaskovic's answer is the classic, but I have a couple to add here as I recently battled a favorite pair of jeans with this problem:
Hairspray. spray it on the zipper, the whole thing. It makes it sticker or something. It works for a period, I had to reapply it every so often. This is more of a stopgap to get the zipper through the day/night.
Tools. Get a pair of pliers, preferable needle nosed. Squeeze each side of the zip mechanism gently to tighten its grip on the teeth. This is delicate careful work, because you can easily break the zip doing this. If you squeeze it too hard you will make it immobile. If you try to open it back up (use a flathead) there is a very high chance you will simply crack the zip. They are cast metal and they don't like to bend. This worked very very well for me, but once again, use caution.
Use a common safety pin.
The very end of your zipper-pull has a tiny hole.
Use a safety pin through that hole and the cloth of the zipper to hold your zipper closed.
You can also use a safety pin through the zipper horizontally below the zipper-pull to prevent it from moving past the pin.