最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I tell the validator of Vuelidate to accept "alphaNum" plus character dot(".&

programmeradmin0浏览0评论

I have an input field, I can tell Vuelidate that it only accepts alphaNum and Required like this:

import { required, alphaNum } from "vuelidate/lib/validators";

export default {
  data() {
    return {
      myInputValue: ""
    };
  },
  validations: {
    myInputValue: {
      required,
      alphaNum
    }
  }
};

Here es my question, how can I make myInputValue to accept an additional character dot(.)?

Which will total accept these things

  1. abcdefghijklmnopqrstuvwxyz
  2. ABCDEFGHIJKLMNOPQRSTUVWXYZ
  3. 0123456789
  4. .

How can I achieve this?

I have an input field, I can tell Vuelidate that it only accepts alphaNum and Required like this:

import { required, alphaNum } from "vuelidate/lib/validators";

export default {
  data() {
    return {
      myInputValue: ""
    };
  },
  validations: {
    myInputValue: {
      required,
      alphaNum
    }
  }
};

Here es my question, how can I make myInputValue to accept an additional character dot(.)?

Which will total accept these things

  1. abcdefghijklmnopqrstuvwxyz
  2. ABCDEFGHIJKLMNOPQRSTUVWXYZ
  3. 0123456789
  4. .

How can I achieve this?

Share Improve this question asked Mar 26, 2020 at 7:04 JosephJoseph 4,7258 gold badges40 silver badges80 bronze badges 2
  • What about .foo? – CertainPerformance Commented Mar 26, 2020 at 7:05
  • .foo will also accept – Joseph Commented Mar 26, 2020 at 7:05
Add a ment  | 

1 Answer 1

Reset to default 9

You can use a regular expression with a character set of alphanumeric characters plus .:

import { required, helpers } from 'vuelidate/lib/validators';
const alphaNumAndDotValidator = helpers.regex('alphaNumAndDot', /^[a-z\d.]*$/i);

export default {
  data() {
    return {
      myInputValue: ""
    };
  },
  validations: {
    myInputValue: {
      required,
      alphaNumAndDotValidator
    }
  }
};

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论