<el-input v-model="scope.row.PRICE" placeholder="请输入价格" value=value.replace(/^(0|[1-9]\d*)(\.\d{1,2})?$/) maxLength="7" ></el-input> 我这样写为什么正则没用呢?
时间: 2024-02-16 16:05:34 浏览: 26
这段代码中的正则表达式是用于限制输入的内容必须为数字,且最多只能有两位小数。但是,这段代码中的正则表达式没有被正确地写入到 `value` 属性中,导致正则无法生效。应该修改为以下方式:
```html
<el-input v-model="scope.row.PRICE" placeholder="请输入价格" :value="value.replace(/^(0|[1-9]\d*)(\.\d{1,2})?$/)" maxLength="7"></el-input>
```
在这里,我们使用了 `:` 绑定 `value` 属性,将其绑定到一个 JavaScript 表达式中。这样,`value` 属性将会被正确地赋值为正则表达式处理后的值,从而实现输入限制的效果。
相关问题
<el-input v-model="scope.row.PRICE" placeholder="请输入价格" oninput="value=value.replace(/^(0|[1-9]\d*)(\.\d{1,2})?$/)" maxLength="7" ></el-input>写法对嘛?正则
这个写法是有一些问题的。
首先,`oninput` 事件应该绑定一个 JavaScript 函数,而不是直接写一个字符串。
其次,`value=value.replace(/^(0|[1-9]\d*)(\.\d{1,2})?$/)` 这段代码中的正则表达式是正确的,可以用来限制只能输入小于1的正数且小数点后只能有两位。但是,在使用这个正则表达式替换输入框中的值时,如果用户输入的值不符合正则表达式,则会被替换为空字符串,这会导致输入框中的值被清空,用户无法输入有效的值。
因此,一个更好的写法是在 `oninput` 事件中调用一个 JavaScript 函数,这个函数可以接受输入框中的值,使用正则表达式验证是否符合要求,如果符合要求,则将值赋给 `v-model` 绑定的属性,否则不做任何操作。下面是一个示例代码:
```html
<el-input v-model="scope.row.PRICE" placeholder="请输入价格" @input="handleInput"></el-input>
```
```javascript
methods: {
handleInput(val) {
const reg = /^(0|[1-9]\d*)(\.\d{1,2})?$/
if (reg.test(val)) {
this.scope.row.PRICE = val
}
}
}
```
这个示例代码中,`handleInput` 函数接受输入框中的值作为参数 `val`,然后使用正则表达式 `reg` 验证这个值是否符合要求。如果符合要求,则将这个值赋给 `v-model` 绑定的属性 `scope.row.PRICE`,否则不做任何操作。这样就可以在不清空输入框中的值的情况下限制输入的内容了。
<template slot-scope="scope"> <el-form-item :prop="'list.' + scope.$index + '.controlok'" :rules="fromDataRules.controlok" > <el-input v-if="scope.row.controluse === 'S2F21'" :disabled=" scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == '' " v-model="scope.row.controlok" filterable clearable /> <el-select v-else v-model="scope.row.controlok" :disabled=" scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == '' " :placeholder="t('web_ems_tip_pleaseInput')" filterable clearable > <el-option v-for="item in identifyingList" :key="item.id" :label="item" :value="item" /> </el-select> </el-form-item> </template>表单验证fromDataRules.controlok自定义若该行的iscontrol的值是true并且controlok的值为空时,提示请输入。代码怎么写
你可以在 `fromDataRules` 中添加一个自定义规则来实现该功能,代码如下:
```javascript
fromDataRules: {
controlok: [
{
required: true,
message: '请输入',
trigger: 'blur',
validator: (rule, value, callback) => {
let index = rule.field.split('.')[1];
let row = this.list[index];
if (row.iscontrol === true && (value === undefined || value.trim() === '')) {
callback(new Error('请输入'));
} else {
callback();
}
}
}
]
}
```
在该规则中,我们定义了一个自定义验证函数 `validator`,它会在输入框失去焦点时被调用。该函数会根据当前行的 `iscontrol` 值和 `controlok` 的值来判断是否需要进行验证。如果需要验证且 `controlok` 值为空,则会调用 `callback(new Error('请输入'))` 来提示用户输入。否则,调用 `callback()` 来表示验证通过。
在模板中,我们需要绑定 `fromDataRules.controlok` 规则,在 `el-form-item` 组件中添加 `prop` 属性,并将其值设为 `'list.' + scope.$index + '.controlok'`,这样该规则就会应用到该行的 `controlok` 属性上。
```html
<template slot-scope="scope">
<el-form-item
:prop="'list.' + scope.$index + '.controlok'"
:rules="fromDataRules.controlok">
<el-input
v-if="scope.row.controluse === 'S2F21'"
:disabled="scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == ''"
v-model="scope.row.controlok"
filterable
clearable />
<el-select
v-else
v-model="scope.row.controlok"
:disabled="scope.row.iscontrol === false || scope.row.iscontrol == null || scope.row.iscontrol == ''"
:placeholder="t('web_ems_tip_pleaseInput')"
filterable
clearable>
<el-option
v-for="item in identifyingList"
:key="item.id"
:label="item"
:value="item" />
</el-select>
</el-form-item>
</template>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)