For the record, they make adjustable depth junction boxes like this.
They allow you to mount the box to a stud, and then adjust the depth at which the box sits on the stud.
The tabs pinch the drywall, and hold the box in place.
Here is what NEC 2008 says...
314.23 Supports. Enclosures within the scope of this article shall be supported in accordance with one or more of the provisions in
314.23(A) through (H).
(A) Surface Mounting. An enclosure mounted on a building or other surface shall be rigidly and securely fastened in place. If the
surface does not provide rigid and secure support, additional support
in accordance with other provisions of this section shall be provided.
(B) Structural Mounting. An enclosure supported from a structural member of a building or from grade shall be rigidly supported either
directly or by using a metal, polymeric, or wood brace.
(1) Nails and Screws. Nails and screws, where used as a
fastening means, shall be attached by using brackets on the outside of
the enclosure, or they shall pass through the interior within 6 mm
(1⁄4 in.) of the back or ends of the enclosure. Screws shall not be
permitted to pass through the box unless exposed threads in the box
are protected using approved means to avoid abrasion of conductor
insulation.
(2) Braces. Metal braces shall be protected against corrosion
and formed from metal that is not less than 0.51 mm (0.020 in.) thick
uncoated. Wood braces shall have a cross section not less than nominal
25 mm × 50 mm (1 in. × 2 in.). Wood braces in wet locations shall be
treated for the conditions. Polymeric braces shall be identified as
being suitable for the use.
(C) Mounting in Finished Surfaces. An enclosure mounted in a finished surface shall be rigidly secured thereto by clamps, anchors,
or fittings identified for the application.
(D) Suspended Ceilings. An enclosure mounted to structural or supporting elements of a suspended ceiling shall be not more than 1650
cm³ (100 in.³) in size and shall be securely fastened in place in
accordance with either (D)(1) or (D)(2).
(1) Framing Members. An enclosure shall be fastened to the
framing members by mechanical means such as bolts, screws, or rivets,
or by the use of clips or other securing means identified for use with
the type of ceiling framing member(s) and enclosure(s) employed. The
framing members shall be adequately supported and securely fastened to
each other and to the building structure.
(2) Support Wires. The installation shall comply with the
provisions of 300.11(A). The enclosure shall be secured, using methods
identified for the purpose, to ceiling support wire(s), including any
additional support wire(s) installed for that purpose. Support wire(s)
used for enclosure support shall be fastened at each end so as to be
taut within the ceiling cavity.
(E) Raceway Supported Enclosure, Without Devices, Luminaires, or Lampholders. An enclosure that does not contain a device(s) other
than splicing devices or support a luminaire(s), lampholder, or other
equipment and is supported by entering raceways shall not exceed 1650
cm³ (100 in.³) in size. It shall have threaded entries or have hubs
identified for the purpose. It shall be supported by two or more
conduits threaded wrenchtight into the enclosure or hubs. Each conduit
shall be secured within 900 mm (3 ft) of the enclosure, or within 450
mm (18 in.) of the enclosure if all conduit entries are on the same
side. Exception: Rigid metal, intermediate metal, or rigid nonmetallic
conduit or electrical metallic tubing shall be permitted to support a
conduit body of any size, including a conduit body constructed with
only one conduit entry, provided the trade size of the conduit body is
not larger than the largest trade size of the conduit or electrical
metallic tubing.
(F) Raceway-Supported Enclosures, with Devices, Luminaires, or Lampholders. An enclosure that contains a device(s), other than
splicing devices, or supports a luminaire(s), lampholder, or other
equipment and is supported by entering raceways shall not exceed 1650
cm³ (100 in.³) in size. It shall have threaded entries or have hubs
identified for the purpose. It shall be supported by two or more
conduits threaded wrenchtight into the enclosure or hubs. Each conduit
shall be secured within 450 mm (18 in.) of the enclosure.
Exception No. 1: Rigid metal or intermediate metal conduit shall be permitted to support a conduit body of any size, including a
conduit body constructed with only one conduit entry, provided the
trade size of the conduit body is not larger than the largest trade
size of the conduit.
Exception No. 2: An unbroken length(s) of rigid or intermediate metal conduit shall be permitted to support a box used for luminaire
or lampholder support, or to support a wiring enclosure that is an
integral part of a luminaire and used in lieu of a box in accordance
with 300.15(B), where all of the following conditions are met:
(a) The conduit is securely fastened at a point so that the length
of conduit beyond the last point of conduit support does not exceed
900 mm (3 ft).
(b) The unbroken conduit length before the last point of conduit
support is 300 mm (12 in.) or greater, and that portion of the conduit
is securely fastened at some point not less than 300 mm (12 in.) from
its last point of support.
(c) Where accessible to unqualified persons, the luminaire or
lampholder, measured to its lowest point, is at least 2.5 m (8 ft)
above grade or standing area and at least 900 mm (3 ft) measured
horizontally to the 2.5 m (8 ft) elevation from windows, doors,
porches, fire escapes, or similar locations.
(d) A luminaire supported by a single conduit does not exceed 300
mm (12 in.) in any direction from the point of conduit entry.
(e) The weight supported by any single conduit does not exceed 9 kg
(20 lb).(f) At the luminaire or lampholder end, the conduit(s) is
threaded wrenchtight into the box, conduit body, or integral wiring
enclosure, or into hubs identified for the purpose. Where a box or
conduit body is used for support, the luminaire shall be secured
directly to the box or conduit body, or through a threaded conduit
nipple not over 75 mm (3 in.) long.
(G) Enclosures in Concrete or Masonry. An enclosure supported by embedment shall be identified as suitably protected from corrosion and
securely embedded in concrete or masonry.
(H) Pendant Boxes. An enclosure supported by a pendant shall comply with 314.23(H)(1) or (H)(2).
(1) Flexible Cord. A box shall
be supported from a multiconductor cord or cable in an approved manner
that protects the conductors against strain, such as a strain-relief
connector threaded into a box with a hub.
(2) Conduit. A box supporting lampholders or luminaires, or wiring
enclosures within luminaires used in lieu of boxes in accordance with
300.15(B), shall be supported by rigid or intermediate metal conduit stems. For stems longer than 450 mm (18 in.), the stems shall be
connected to the wiring system with flexible fittings suitable for the
location. At the luminaire end, the conduit(s) shall be threaded
wrench tight into the box or wiring enclosure, or into hubs identified
for the purpose. Where supported by only a single conduit, the
threaded joints shall be prevented from loosening by the use of
setscrews or other effective means, or the luminaire, at any point,
shall be at least 2.5 m (8 ft) above grade or standing area and at
least 900 mm (3 ft) measured horizontally to the 2.5 m (8 ft)
elevation from windows, doors, porches, fire escapes, or similar
locations. A luminaire supported by a single conduit shall not exceed
300 mm (12 in.) in any horizontal direction from the point of conduit
entry.
More useful information.