Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear the field automatically if it is not completely filled #51

Open
diancabral opened this issue Dec 13, 2017 · 3 comments
Open

Clear the field automatically if it is not completely filled #51

diancabral opened this issue Dec 13, 2017 · 3 comments

Comments

@diancabral
Copy link

diancabral commented Dec 13, 2017

Good evening.

Is there any way to automatically clear the field when the mask is not completely filled?

In the jQuery Mask Plugin of Igor Escobar (https://github.com/igorescobar/jQuery-Mask-Plugin) there is the "clearIfNotMatch" option that does just that.

I need this option to "force" the user to fill in the data correctly because the field is accepting incomplete data.

@diancabral diancabral changed the title Limpar o campo automaticamente se não for preenchido completamente Clear the field automatically if it is not completely filled Dec 13, 2017
@hardrese7
Copy link

hardrese7 commented Apr 5, 2019

Hey there,
you can easily implement that feature by yourself.

maskedInput.vue

<template>
  <input-el
    :value="value"
    @input="$emit('input', $event)"
    @blur="clearIncompletedField()"
    @paste.prevent
    v-mask="mask"
    v-bind="$attrs"
  />
</template>

<script>
import { mask } from 'vue-the-mask';

export default {
  inheritAttrs: false,
  directives: { mask },
  props: {
    value: String,
    mask: String,
  },
  methods: {
    clearIncompletedField() {
      if (this.value.length < this.mask.length) {
        this.$emit('input', '');
      }
    },
  },
};
</script>

input-el is just my wrapper of the native html input

@hardrese7
Copy link

updated example:

<template>
  <input-el
    v-mask="mask"
    :value="value"
    v-bind="$attrs"
    @input="$emit('input', $event)"
    @blur="clearIncompletedField()"
    @paste.prevent
  />
</template>

<script>
import { mask } from 'vue-the-mask';

export default {
  directives: { mask },
  inheritAttrs: false,
  props: {
    value: {
      type: String,
      required: true,
    },
    mask: {
      type: [String, Array],
      required: true,
    },
  },
  methods: {
    clearIncompletedField() {
      if (this.mask) {
        if (Array.isArray(this.mask)) {
          const valueMatchesMask = this.mask.some(m => this.value.length === m.length);
          if (valueMatchesMask) {
            return;
          }
          this.$emit('input', '');
        } else if (this.value.length < this.mask.length) {
          this.$emit('input', '');
        }
      }
    },
  },
};
</script>

@rfranchi
Copy link

updated example:

<template>
  <input-el
    v-mask="mask"
    :value="value"
    v-bind="$attrs"
    @input="$emit('input', $event)"
    @blur="clearIncompletedField()"
    @paste.prevent
  />
</template>

<script>
import { mask } from 'vue-the-mask';

export default {
  directives: { mask },
  inheritAttrs: false,
  props: {
    value: {
      type: String,
      required: true,
    },
    mask: {
      type: [String, Array],
      required: true,
    },
  },
  methods: {
    clearIncompletedField() {
      if (this.mask) {
        if (Array.isArray(this.mask)) {
          const valueMatchesMask = this.mask.some(m => this.value.length === m.length);
          if (valueMatchesMask) {
            return;
          }
          this.$emit('input', '');
        } else if (this.value.length < this.mask.length) {
          this.$emit('input', '');
        }
      }
    },
  },
};
</script>

Thank you for the example, i had to adjust the @input="$emit('input', $event)" to @input="$emit('input', $event.target.value)" because was freezing the browser. After that worked perfectly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants