Lina Basuni
Lina Basuni

Reputation: 77

Custom validation 1000hz bootstrap validator

I implemented a custom validation function and it validates correctly but the problem is I can submit the form successfully even if the field is invalid.

this is my code:

$("#applicant-email").change(function() {

  var restURL= "http://apilayer.net/api/check?access_key=<key>&email="+$("#applicant-email").val()+"&smtp=1&format=1";
  $.ajax({
    type : 'GET',
    url :restURL,
    dataType :'json',
    success : renderList,
  });
  return false;
});



function renderList(data) {
  if ((data.format_valid == true) && (data.smtp_check == true) ) {
    console.log("Email address is valid");
    $("#applicant-email").removeClass("email-invalid");
    $("#applicant-email").addClass("email-valid");
  }
  else {
    console.log("Email address is not valid");
    $("#applicant-email").removeClass("email-valid");
    $("#applicant-email").addClass("email-invalid");

  }
}


and the custom validator function:


$('#myForm').validator({
  custom: {
    emailexist: function ($el) {
      if($('#'+$el.attr('id')).hasClass('email-invalid')){
        console.log("it is invalid")
        return true;
      }
      else {
        console.log("it is valid");
        return false;
      }
    }


  },
  errors: {
    emailexist: "Nope"
  }
})

and the input field html code :

<input id="applicant-email" required data-table="Applicant email address" data-emaildup="false" data-emailexist="email" data-emailexist-error="Enter an existant email" data-emaildup-error="You can't use the same email address more than once" data-pattern-error="Please enter a valid email address" data-required-error="Please enter an Email address."  name="applicant-email" type="email" class="multisteps-form__input form-control"  placeholder="Email address">
<div class="help-block with-errors"></div>

Upvotes: 0

Views: 345

Answers (0)

Related Questions