You are viewing limited content. For full access, please sign in.

Question

Question

check if two fields match

asked on August 19, 2019 Show version history

I am trying to check if two fields match! the requester name can not be the same as the approver name!

 

$(document).ready(function () {
  $('.Submit').hide(); //hide the submit button by default
  
   $Field28.change(function() {

// this is the email field that when its changed the name fields are validated! hide submit button if they match!!!


  var name1 = document.getElementById(".RName").value;
  var name2 = document.getElementById(".AName").value;
  var $Field28 = $('#Field28');
   
     if(name1 == name2) {
    $('.Submit').hide();
    
  }else {
     $('.Submit').show();
  
                  
});

0 0

Answer

SELECTED ANSWER
replied on August 19, 2019

Why do you want to hide the submit button?

If the error message is present, then pressing the submit button will just re-run the validation and put the focus back on the invalid field.

If you really want to hide the submit button, you're going to need to monitor change events and write an entirely separate validation process similar to what Matthew suggested.

0 0

Replies

replied on August 19, 2019

Are AName and RName CSS Class names on the fields?  If so, it may work like this (I haven't tested):

$(document).ready(function () {

  $('.Submit').hide(); //hide the submit button by default
  
   $('#Field28').change(function() {
      // this is the email field that when its changed the name fields are validated! hide submit button if they match!!!

      var name1 = $('.RName input').val();
      var name2 = $('.AName input').val();  
      if(name1 == name2) {
        $('.Submit').hide();
      }
      else {
        $('.Submit').show();
      }

  });

});

 

0 0
replied on August 19, 2019

yes the are a css class

0 0
replied on August 19, 2019 Show version history

Better yet, you can use a custom validator to make sure they don't match. Not only would that prevent submission, but it would also include all of the validation functionality like an error message and focusing on the affected fields.

  // custom validator for name confirmation
  window.Parsley.addValidator('uniquename', {
    validateString: function(value, requirement, field) {
      var rName = $('.RName input').val().toLowerCase();
      var aName = $('.AName input').val().toLowerCase();
      
      return (rName != aName || rName == '' || aName == '');
    },
    messages: {
      en: 'Requester name cannot match approver name.',
    }
  });
  
  // assign validator to name fields
  $('.RName input, .AName input').attr('data-parsley-uniquename','');

 

0 0
replied on August 19, 2019

the message is working! the problem is the submit button still shows up! when the emails match! it should be hidden 

0 0
SELECTED ANSWER
replied on August 19, 2019

Why do you want to hide the submit button?

If the error message is present, then pressing the submit button will just re-run the validation and put the focus back on the invalid field.

If you really want to hide the submit button, you're going to need to monitor change events and write an entirely separate validation process similar to what Matthew suggested.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.