A federal judge refused Friday to block California’s new vaccination law, which requires children in public and private schools to be inoculated against 10 contagious illnesses and eliminates an exemption based on their parents’ personal beliefs.

“Society has a compelling interest in fighting the spread of contagious diseases through mandatory vaccination of school-age children,” U.S. District Judge Dana Sabraw of San Diego said in denying an injunction against enforcement of the law, sought by 17 parents and four antivaccine organizations.

The right to freely practice one’s religion, one of the rights invoked by opponents of the new law, “does not outweigh the state’s interest in public health and safety,” Sabraw said.

The law was prompted by a measles outbreak in 2014 that was traced to youngsters at Disneyland who hadn’t been vaccinated. Implemented last month, the new measure makes California one of three states, along with West Virginia and Mississippi, to require all schoolchildren to be vaccinated against illnesses such as measles, mumps, tetanus and rubella, regardless of their parents’ religious or personal opposition. The only exceptions are for students with doctor-certified medical exemptions and for disabled students in individual education programs.

While the law says that all children must be vaccinated, it requires parents to provide immunization records only when a student is entering kindergarten or the seventh grade. That means an elementary-school student with a previous parental exemption does not need to be vaccinated until the seventh grade, and students in the eighth grade or higher with a past exemption won’t require any vaccinations.

Plaintiffs in the lawsuit say 33,000 students in California whose parents oppose vaccinations are enrolling in either kindergarten or the seventh grade and will be denied enrollment unless they agree to be vaccinated.

The law “has made second-class citizens out of children who for very compelling reasons are not vaccinated,” plaintiffs’ attorney Robert Moxley said in announcing the suit July 1. Opponents went to court after failing to qualify a state ballot referendum to undo the law.

Sabraw, in his ruling, said the California Supreme Court had upheld mandatory vaccination for schoolchildren as long ago as 1890. He also cited a 1944 U.S. Supreme Court ruling that prohibited a young girl from distributing religious literature on the streets in violation of a Massachusetts child-labor law. The family’s claim of religious freedom in that case “does not include liberty to expose the community or the child to communicable disease,” the high court said.

The right to an education, strongly protected by California law, must give way to the public interest in protecting children’s health, he said.

State Sen. Richard Pan, D-Sacramento, sponsor of the vaccine law, praised the ruling and said that “our schools are already becoming safer” because of the strengthened vaccination requirement.

Attorney Kim Mack Rosenberg said the plaintiffs would appeal. “All children in California deserve to receive the education that is their guaranteed right,” a right they will lose if the vaccination law is enforced, she said.

Bob Egelko is a San Francisco Chronicle staff writer. Email: begelko@sfchronicle.com Twitter: @egelko