Javascript Web Form Validattion

I am looking for some help with a web form using JavaScript for validation. This is a section of a larger form that was created in a CMS. (hence the extra code padding)

The form has a question that asks the user if they have a Social Security Number. (SocialSecurity field) It has two radio buttons, one yes and one no. IF the user answers yes, then another field (SSN) becomes visible. Otherwise, it stays hidden. This part is actually working. (the show/hide of the SSN field)

The JS needs to do two things. IF the user answers yes, then the SSN field becomes required, and must validate that it has 9 integers. BUT, if the user answers no, then SSN field, which will stay hidden must no longer be required.

What I am having difficulty with, is getting the SSN to be required only if the user answers yes, and validate it correctly under that circumstance.

Here is the HTML code:

`


Do you have a Social Security Number?
As a U.S. Citizen, permanent resident, or temporary working resident, I have a Social Security Number.
Due to my international student status, my residency status, or my specific visa type, I do not have a Social Security Number.
Social Security Number
`

Here is the JavaScript:

function validateSSN() {
  if (!validateRequiredTextField('SSN', 'Social Security Number'))
    return false;

  var value = $('.field.SSN input').val();

  if (!isInteger(value) ||
    (value.length != 9) ||
    (value == '000000000') ||
    (value == '111111111') ||
    (value == '222222222') ||
    (value == '333333333') ||
    (value == '444444444') ||
    (value == '555555555') ||
    (value == '666666666') ||
    (value == '777777777') ||
    (value == '888888888') ||
    (value == '999999999') ||
    (value == '123456789')) {
    $('.field.SSN input').focus();
    alert("Invalid: Social Security Number");
    return false;
  }

  return true;
}

//overall form validation - section on SSN
function validateForm()

if (!validateSSN())
  return false;

Also on: https://jsfiddle.net/Ladybug333/acrd4w0n/11/#&togetherjs=KBFMVaj2wS

Sounds like you want to control the ‘required’ attribute on the $(’.field.SSN input’).

Here’s a whole thing about it at StackOverflow: