Not allowing N/A entries on some fields.

bfuchs
bfuchs used Ask the Experts™
on
Hi Experts,

I have the following JS code set certain fields as required.

// set required to true or false
function setRequired(flag) {
  if (flag == true) {
    // get the current sig, default = []
    var sig = $('input[name="output"]').val();
    $('#cb_sign_wrapper').signaturePad({drawOnly : true}).regenerate( (sig) ? sig : [] );

    $('.req').each(function() {
      $(this).closest('tr').next().find('input').attr('required',true);
    });

    $('.req').each(function() {
      $(this).closest('tr').next().find('textarea').attr('required',true);
    });

    $('.req').each(function() {
        $(this).closest('tr').next().find('select').attr('required',true);
    });
  } else {

    $('input:required').each(function() {
      $(this).prop('required',false);
    });

    $('textarea:required').each(function() {
      $(this).prop('required',false);
    });

    $('select:required').each(function() {
      $(this).prop('required',false);
    });
  }
}

Open in new window


Wondering if there is a way to prohibit users from entering N/A or NA on these fields?

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
That should do it:

// set required to true or false
function setRequired(flag) {
  if (flag == true) {
    // get the current sig, default = []
    var sig = $('input[name="output"]').val();
    $('#cb_sign_wrapper').signaturePad({drawOnly : true}).regenerate( (sig) ? sig : [] );

    $('.req').each(function() {
      $(this).closest('tr').next().find('input').attr('required',true);
    });

    $('.req').each(function() {
      $(this).closest('tr').next().find('textarea').attr('required',true);
    });

    $('.req').each(function() {
        $(this).closest('tr').next().find('select').attr('required',true);
    });
  } else {

    $('input:required').each(function() {
      $(this).prop('required',false);
    });

    $('textarea:required').each(function() {
      $(this).prop('required',false);
    });

    $('select:required').each(function() {
      $(this).prop('required',false);
    });
  }


	$(":input[required]:visible").on("change", function() {
	    if($(this).val().replace(/\s/g,"") == "N/A") {
	        $(this).val(""); // empty the field
	        alert("N/A is not a valid input");
	        $(this).focus(); // put back focus to the field
	    }
	});




}

Open in new window

Hi Leakim,

Will that also prevent users from entering "NA" in those fields?

Thanks,
Ben
Hi,

Don't forget to validate using server side (PHP/ASP) not only using Javascript as user can bypass Javascript validation from the browser.

You can hide the field using server side code in some case if you don't want user to fill the field
or set the field  to disabled.
Again don't forget to validate this using Server side to make sure no data or correct data is entered.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Multitechnician
Top Expert 2014
Commented:
Will that also prevent users from entering "NA" in those fields?

no, here a version with NA, N/A, N//A :

// set required to true or false
function setRequired(flag) {
  if (flag == true) {
    // get the current sig, default = []
    var sig = $('input[name="output"]').val();
    $('#cb_sign_wrapper').signaturePad({drawOnly : true}).regenerate( (sig) ? sig : [] );

    $('.req').each(function() {
      $(this).closest('tr').next().find('input').attr('required',true);
    });

    $('.req').each(function() {
      $(this).closest('tr').next().find('textarea').attr('required',true);
    });

    $('.req').each(function() {
        $(this).closest('tr').next().find('select').attr('required',true);
    });
  } else {

    $('input:required').each(function() {
      $(this).prop('required',false);
    });

    $('textarea:required').each(function() {
      $(this).prop('required',false);
    });

    $('select:required').each(function() {
      $(this).prop('required',false);
    });
  }


	$(":input[required]:visible").on("change", function() {
	    if($(this).val().replace(/[\s\/]/gi,"").toLowerCase() == "na") {
	        $(this).val(""); // empty the field
	        alert("N/A is not a valid input");
	        $(this).focus(); // put back focus to the field
	    }
	});




}

Open in new window

@Lenamtl,

Again don't forget to validate this using Server side to make sure no data or correct data is entered.
I'm using Caspio tools for it, and therefore not in control of server side programming.

@Leakim,
Will test yours tom.

Thanks,
Ben
This works
Thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial