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

Question

Question

Best way to validate increments of 4

asked on April 30, 2019

I have a number field and it can only accept increments of 4 like 4,8,12, etc.. What is the best way to validate this in the form?

 

Thanks

Priya

0 0

Replies

replied on April 30, 2019

check out the javascript modulus operator. It finds the remainder.

psuedo code below:

var n = myInput;

if (n % 4 == 0) {

Yep it's valid

}

0 0
replied on April 30, 2019

Thanks

0 0
replied on April 30, 2019

No problem. And I pulled that out of my head. It should have read modulo operator.

0 0
replied on May 1, 2019

Is there a way, we can do this using Forms error message or process error message please?

0 0
replied on May 1, 2019 Show version history

Hmm, I don't think so. This would be a javascript solution to the problem.

Here you go, assuming that you are using a "text under field option"

css

/* int-4 (could be named anything) is a class assigned to the input field you are targeting and the un-help class targets the text under lable */

.int-4 .un-help {
      color: red;
  display: none;
}

jquery

$(document).ready(function () {

// When the targeted field changes...

$(".int-4").change(function(){

// find the value of the field

 	var n = $(".int-4 input").val();
// make sure it's not empty
 if (n > 0) {
// find the remainder of the value divided by 4
    var r = n % 4;

// if it isn't 0, then show the text under label that says
// something like " Must be increments of 4!" and removes the 
// input value
    if (r !== 0 ) {
      $('.int-4 .un-help').show();
      $(".int-4 input").val('');
    }

// if the remainder is 0, hide the warning and move along.
    else {
      $('.int-4 .un-help').hide();
    }
  }
});
});

 

0 0
replied on May 2, 2019

Thanks, will try

1 0
replied on May 2, 2019

I added some documentation in case you're unfamiliar with javascript.

0 0
replied on May 2, 2019

Thanks

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

Sign in to reply to this post.