坐标验证

huangapple go评论73阅读模式
英文:

Coordinates validation

问题

<ValidationProvider v-slot="{ errors }" rules="coordinates_validation">
            <v-text-field
              :label="$t('stations.position')"
              :value="positionValue"
              :error-messages="errors"
              @input="$emit('update:station', { ...station, ...getLatLong($event) })"
              @keypress="justNumber"
            />
</ValidationProvider>
extend('coordinates_validation', {
  validate: (value) => {
    const coordinates = value.split(',');

    if (coordinates.length !== 2) {
      return false;
    }

    const trimmedCoordinates = coordinates.map((coord) => coord.trim());

    const isValidCoordinate = (coord) => {
      return !Number.isNaN(parseFloat(coord)) && Number.isFinite(parseFloat(coord));
    };

    return (
      trimmedCoordinates.every(isValidCoordinate) &&
      !trimmedCoordinates.some((coord) => coord === "")
    );
  },
  message: i18n.tc('validations.coordinates_incorrect_format'),
});
英文:

I have text field for coordinates and i would like to vlaidate it using vee-validate (3.x) and vue 2. I have tried two different aproches, but neither of them work. The format should be in format "int or float,int or float" "latitude,longitude" (with only one coma, multiple comas should be flagged as invalid)

This is the textfield:

<ValidationProvider v-slot="{ errors }" rules="coordinates_validation">
            <v-text-field
              :label="$t('stations.position')"
              :value="positionValue"
              :error-messages="errors"
              @input="$emit('update:station', { ...station, ...getLatLong($event) })"
              @keypress="justNumber"
            />
</ValidationProvider>

These are the two aproches i have tried, but neither of them work:

extend("coordinates_validation", {
  validate: (value) => {
    const coordinates = value.split(",");

    if (coordinates.length !== 2) {
      return false;
    }

    const trimmedCoordinates = coordinates.map((coord) => coord.trim());

    const isValidCoordinate = (coord) => {
      return !Number.isNaN(parseFloat(coord)) && Number.isFinite(coord);
    };

    return (
      trimmedCoordinates.every(isValidCoordinate) &&
      !trimmedCoordinates.some((coord) => coord === "")
    );
  },
  message: i18n.tc("validations.coordinates_incorrect_format"),
});
extend('coordinates_validation', {
  validate: (value) => {
    const regex = /^\d+(\.\d+)?,\s*\d+(\.\d+)?$/;
    return regex.test(value);
  },
  message: i18n.tc('validations.coordinates_incorrect_format'),
});

Does anybody know how to solve this?

答案1

得分: 1

在疑惑时,将其记录在控制台中

查看我的 codesandbox,我在其中记录了您的验证的每个步骤,必要时将大步骤拆分为小步骤。

对于您的第一次尝试,问题在于 Number.isFinite 始终返回 false。这是因为您将其传递了一个字符串 coord,但 Number.isFinite 期望一个数字。修复方法如下:

Number.isFinite(parseFloat(coord))

您最初的 value.split 仅适用于逗号 ,。我建议使用空格 、逗号 , 和逗号 + 任意数量的空格 , 来拆分:

const coordinates = value.split(/[,\s]\s*/);

对于您的第二次尝试,仅使用正则表达式,我看不出有任何问题。我将代码放在同一个 codesandbox 中,当作为验证方法使用时,它完美地工作。

英文:

When in doubt, console.log it out

See my codesandbox where I console.log each step of your validation, splitting big steps into smaller steps where necessary.

For your first attempt, the problem is Number.isFinite always returning false. This is because you feed it a string coord but Number.isFinite expects a number. The fix is then:

Number.isFinite(parseFloat(coord))

Your initial value.split also only works with comma ,. I suggest splitting on space , comma , and comma + any amount of space ,

const coordinates = value.split(/[,\s]\s*/);

For your second attempt using just regex, I see nothing wrong. I put the code in the same codesandbox and when used as the validation method it works perfectly.

huangapple
  • 本文由 发表于 2023年7月17日 17:24:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定