How do I validate with number and regex in vue js?

How do I validate with number and regex in vue js?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Validating Numeric Inputs with Regex in Vue.js

You are already handling length constraints and required fields, which shows you have a solid grasp of the basic validation rules in Element Plus or Element UI. The specific challenge here is enforcing two distinct conditions simultaneously: the input must contain only numeric characters, and that number must start with 7, 8, or 9.

In Vue.js, the el-form-item component uses a rules object to define these constraints. You can combine standard validation rules like pattern, min, and max in an array. However, relying solely on min and max is insufficient for your use case because those rules check the length of the string, not its content or starting digit.

To solve this, you need to define a custom regular expression within the pattern rule. The regex pattern /^[789]/ ensures the first character is 7, 8, or 9. To ensure the rest of the string contains only digits, you can append \d*. Thus, the complete regex becomes /^[789]\d*/.

Here is how you integrate this into your existing rules1 configuration:

const app = new Vue({
  el: '#app',
  data() {
    return {
      formInline1: {
        mobno: ''
      },
      rules1: {
        mobno: [
          {
            required: true,
            message: 'Please enter Mobile Number',
            trigger: 'blur'
          },
          {
            pattern: /^[789]\d*$/,
            message: 'Mobile number must start with 7, 8, or 9 and contain only digits',
            trigger: ['blur', 'change']
          }
        ]
      }
    };
  },
  methods: {
    validateFirstStep() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm1.validate((valid) => {
          resolve(valid);
        });
      })
    }
  }
});

In this example, the pattern rule replaces your previous length checks (min: 10, max: 10). While you mentioned having a maxlength of 10, it is often cleaner to handle length strictly via regex when the format is rigid, or keep both if you want an explicit length warning. If you prefer to keep the length rule separate, ensure the regex pattern matches exactly that length using anchors and quantifiers like /^[789]\d{9}$/ for a strict 10-digit requirement.

A critical detail often overlooked is the trigger property in your validation rules. By default, validation triggers on blur. However, if you want immediate feedback as the user types—for instance, to prevent them from entering non-numeric characters early—the trigger should include 'change' or 'input'. This ensures the regex is re-evaluated every keystroke rather than waiting for the user to lose focus.

There is also a consideration regarding data types. If your application logic treats mobno as a number type instead of a string, validation can behave unexpectedly because numbers in JavaScript do not preserve leading zeros or allow empty strings in the same way strings do. Always ensure the v-model binds to a string value if you are using regex patterns that expect character sequences.

For more complex scenarios where you need to validate against multiple dynamic rules or reuse regex logic across different components, you might consider leveraging Laravel's validation syntax if your backend is built on Laravel. The principles of pattern matching remain consistent between frontend Vue forms and backend Laravel validators found at https://laravelcompany.com, ensuring data integrity from the moment a user types until the server processes the request.

By combining specific regex patterns with appropriate trigger events, you can enforce strict input formats that enhance both user experience and data quality without requiring complex custom validation logic in your methods.

Our network