Skip to content

Element-Plus Form Validation

This guide will help you understand how to use the isInRangeForElementPlus function for form validation in Element-Plus.

Syntax

ts
isInRangeForElementPlus(name, options, lang);
isInRangeForElementPlus(name, options, lang);

Parameters

  • name (string): The name of the field to be validated.
  • options (object): An object containing validation rules. The properties of this object are optional and are as follows:
PropertyDescription
positiveIf true, the value must be a positive number
integerIf true, the value must be an integer
positiveIntegerIf true, the value must be a positive integer
negativeIf true, the value must be a negative number
negativeIntegerIf true, the value must be a negative integer
decimalThe maximum number of decimal places allowed
minThe minimum value allowed
maxThe maximum value allowed
unitThe unit of the value, used in error messages
  • lang (string): The language of the error messages. It can be either 'en' for English or 'zh' for Chinese.

Usage

The isInRangeForElementPlus function is used as a custom validator in Element-Plus form validation. It checks if the input value is within a certain range and meets certain conditions specified in the options parameter.

Amount
数量

Here is an example of how to use it:

Vue
<template>
  <el-form :model="form" :rules="rules">
    <el-form-item label="Amount" prop="amount1">
      <el-input v-model="form.amount1"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="amount2">
      <el-input v-model="form.amount2"></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import {ref} from 'vue'
import { ElInput } from 'element-plus'
import { isInRangeForElementPlus } from 'pixiu-number-toolkit-vue';

const form=ref({amount1:'',amount2:''})
const rules={
  amount1: [
    {
      required:true,
      validator: isInRangeForElementPlus('Amount', { min: 0, max: 100, decimal: 2 }, 'en'),
      trigger: 'blur'
    },
  ],
  amount2: [
    {
      required:true,
      validator: isInRangeForElementPlus('数量', { min: 0, max: 100, decimal: 2 }, 'zh'),
      trigger: 'blur'
    },
  ],
}
</script>
<template>
  <el-form :model="form" :rules="rules">
    <el-form-item label="Amount" prop="amount1">
      <el-input v-model="form.amount1"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="amount2">
      <el-input v-model="form.amount2"></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import {ref} from 'vue'
import { ElInput } from 'element-plus'
import { isInRangeForElementPlus } from 'pixiu-number-toolkit-vue';

const form=ref({amount1:'',amount2:''})
const rules={
  amount1: [
    {
      required:true,
      validator: isInRangeForElementPlus('Amount', { min: 0, max: 100, decimal: 2 }, 'en'),
      trigger: 'blur'
    },
  ],
  amount2: [
    {
      required:true,
      validator: isInRangeForElementPlus('数量', { min: 0, max: 100, decimal: 2 }, 'zh'),
      trigger: 'blur'
    },
  ],
}
</script>

Released under the MIT License.