-1

I'm trying to display a Javascript error message if a certain range value is entered in a field. example: If the version entered is between 13.0.x to 13.8.5, an error message will be displayed. I'm trying the code below but it's not working. Any help would be greatly appreciated. Thank you

$('#custom_fields_1234').on('blur', function() {
      $('.errorMessage').remove();
      if ($(this).val().split('.')[0] < val => 13.0.0 && =< 13.8.5) {
        $('.custom_fields_1234').append(errorMessage());
      }
OSG
  • 1
  • 3
  • Please edit your question and add the **minimal required** HTML to reproduce your problem. – connexo Jan 07 '22 at 06:08
  • Please do not repeat asking [the very same question](https://stackoverflow.com/questions/70616515/if-certain-version-is-entered-display-a-javascript-error-message) over and over if your initial question gets closed. Instead, try to remedy the shortcomings pointed out in the close reason by editing your initial question. – connexo Jan 07 '22 at 06:31

1 Answers1

1

You can split the version number to check, for example.

function isVersionInRange( version, maxVersion, minVersion ) {
  const [major, minor, patch] = version.split('.').map(Number)
  const [maxMajor, maxMinor, maxPatch] = maxVersion.split('.').map(Number)
  const [minMajor, minMinor, minPatch] = minVersion.split('.').map(Number)

  // if the version is greater than the maxVersion
  if (
    major > maxMajor ||
    (major === maxMajor && minor > maxMinor) ||
    (major === maxMajor && minor === maxMinor && patch > maxPatch)
  ) {
    return false
  }

  // if the version is less than the minVersion
  if (
    major < minMajor ||
    (major === minMajor && minor < minMinor) ||
    (major === minMajor && minor === minMinor && patch < minPatch)
  ) {
    return false
  }

  return true
}

Hao-Jung Hsieh
  • 516
  • 4
  • 9