regex cleanup

 avatar
user_8454105
plain_text
11 days ago
1.3 kB
1
Indexable
Never
export default {
  data() {
    return {
      contact: {
        address1: '',
        address2: '',
        billing: {
          address1: '',
        }
      },
    };
  },
  methods: {
    // Function to remove unwanted characters
    removeUnwantedChars(input) {
      const regex = /[\*\.,\(\)\"\'\:\;\-\@\&]| {2,}/g;
      return input.replace(regex, '');
    },
    
    // Utility function to set a nested value by path
    setNestedValue(obj, path, value) {
      const keys = path.split('.');
      let target = obj;
      for (let i = 0; i < keys.length - 1; i++) {
        target = target[keys[i]];
      }
      target[keys[keys.length - 1]] = value;
    },
    
    // Utility function to get a nested value by path
    getNestedValue(obj, path) {
      return path.split('.').reduce((acc, key) => acc[key], obj);
    },

    // Generic input handler to clean unwanted chars and trigger validation
    handleInput(fieldPath) {
      const currentValue = this.getNestedValue(this, fieldPath);
      const cleanedValue = this.removeUnwantedChars(currentValue);
      this.setNestedValue(this, fieldPath, cleanedValue);

      // Trigger the validation after cleaning
      this.$v[fieldPath].$touch();
    }
  }
};
Leave a Comment