Email Validation in Jquery based on the yes no choice


Email validation using Jquery by yes no radio button action
The following are the code snippet of the radio button and email text field
<div class="radioGroup">

  <label class="control-label" ><spring:message code="email.choice"/></label>

  <div class="controls">

  <input type="radio" name="emailChoice" id="emailChoice1" value="yes" /><label><spring:message code="yes"/></label><br>

  <input type="radio" name="emailChoice" id="emailChoice2" value="no" /><label><spring:message code="no"/></label><br>

  </div>

</div>
<div class="form-group">
  <label class="control-label">Email:</label>
  <div class="controls">
  <input name="email" id="email" class="FormField" type="text" size="30" maxlength="60">
  </div>
</div>
<div class="form-group">
  <label class="control-label">Re-enter Email:</label>
  <div class="controls">
  <input name="emailConfirmation" id="emailConfirmation" class="FormField" type="text" size="30" maxlength="60">
  </div>
</div>
The following are the Jquery code email validation requied only checked on the email choice yes
<script>

$(function() {

    $("#formName").validate({

        errorElement:'div',

        rules: {

          email:

                {

                    required:"#emailChoice1:checked",

                    email: true

                },

            emailConfirmation:

                {

                    required:"#emailChoice1:checked",

                    equalTo: "#email"

                }

        },

    messages: {

            email:

                {

                    required: "Email required.",

                    email: "Enter valid email."

                },

            emailConfirmation: {

                required: "Email required.",

                equalTo: "Email does not match."

            }

        },

       submitHandler: function(form) {

            form.submit();

        }

    });

});


</script>

Comments