1

I am trying to get my vuetify form to validate my text box on the following criteria "AB-12345678" or if user forgos the hyphen and just puts in a number with a max of 8 digits. Can regex rules account for both of these scenarios in a single expression? My code so far:

        <v-form ref="sidebarSearchForm" lazy-validation v-on:submit.prevent>
        @Html.AntiForgeryToken()
        <v-text-field label="Search"
                      single-line
                      filled
                      rounded
                      dense
                      append-icon="mdi-magnify"
                      v-model="id"
                      :rules"[v => !!v || 'ID Required!']"
                      @@click:append="searchID"
                      v-bind="{error: !validationProp,...(!validationProp && { 'error-messages': ['ID not found!'] })}"></v-text-field>
    </v-form>
1
  • regex to match both AB-12345678 and 12345678, right? Commented Sep 20, 2022 at 2:50

3 Answers 3

1

Here is the full example: using this regex

^([a-zA-Z]{2}-\d{0,8}$|^\d{0,8}$)
  • AA1221212 (Not valid)
  • AA-111 (Valid)
  • AA-12345678 (Valid))
  • ABC-12345678 (Not valid)
  • 12345678 (Valid)
  • 123456789 (Not valid)
  • 123 (Valid)

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    valid: true,
    id: '',
    idRules: [
      v => !!v || 'Field is required',
      v => /^([a-zA-Z]{2}-\d{0,8}$|^\d{0,8}$)/.test(v) || 'Expression must be valid'
    ],
   
  }),

  methods: {
    validate () {
      this.$refs.form.validate()
    },
    reset () {
      this.$refs.form.reset()
    },
    resetValidation () {
      this.$refs.form.resetValidation()
    },
  },
})
   
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<div id="app">
  <v-app>
    <v-main>
      <v-container>
      
<v-form
      ref="form"
      v-model="valid"
      lazy-validation
    >
      <v-text-field
        v-model="id"
        :counter="10"
        :rules="idRules"
        label="Search"
        required
      ></v-text-field>
  
     
      <v-btn
        :disabled="!valid"
        color="success"
        class="mr-4"
        @click="validate"
      >
        Validate
      </v-btn>
  
      <v-btn
        color="error"
        class="mr-4"
        @click="reset"
      >
        Reset Form
      </v-btn>
  
      <v-btn
        color="warning"
        @click="resetValidation"
      >
        Reset Validation
      </v-btn>
    </v-form>
      
      </v-container>
    </v-main>
  </v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>

Sign up to request clarification or add additional context in comments.

2 Comments

I was trying to go for AA-12345678 (full format with two character ID with hyphen then max 8 digit number) or 12345678 or 123 or 123 (any number but max 8 digits). Your regex makes sense but it doesnt account for just numbers by themselves max 8 digits.
Check now I update the regex
0

You can add multiple rules using regex or simple like this:

(Update the regex as per your requirement)

 <v-text-field
      v-model="id"
      :rules="idRules"
      label="Search"
      required
    ></v-text-field>


data: () => ({
        idRules: [
        v => !!v || 'Field is required',
        v => /.+@.+\..+/.test(v) || 'Expression must be valid',
      ],
  }),

2 Comments

Can you add the regex expression for this use case above and I will accept this answer. I was struggling with that aspect actually .
Yes I added full code for you (For a better view you can see it on the full page )
0

If you want to match both (2 letters)-(8 digits) and (8 digits)

Try this:

^([a-zA-Z]{2}-\d{8})$|^(\d{8})$

In vue rules:

v => /^([a-zA-Z]{2}-\d{8})$|^(\d{8})$/.test(v) || 'Expression must be valid',

https://regex101.com/r/37EqR9/1

1 Comment

THe first part is correct but the second option should be any number less than 999999999 in any form just a digit though. So 1234 or 00001234. That website is pretty neat though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.