如何检验文本域里字段(如气温)进行提示?

作者站长头像
站长
· 阅读数 14

检测文本框中的信息字段内容,主要是温度内容,温度是27-35℃,前面温度要低于后面温度,最低不低于-2℃,最高不高于40℃。发现出现异常的信息字要进行提示。这种如何实现呢?

如:预计今天6:30-9:00,贵阳市城区多云,气温26-269℃。其中269 是异常信息(根据Riki一二三的回复 我目前实现了零上的判断和单个℃判断)如果输入内容为:气温为-2--1℃ 或者气温-2-0℃的提取 数值的方法

想要的效果是如上诉求.

'temp.content':{
  handler(newV, oldV) {//适合单个-含多个℃不适用
    const reg = /-?(\d+)?-?(\d+)℃/
    if(reg.test(newV)){
      let item = newV.match(/-?(\d+)?-?(\d+)℃/)
      let preTemp = item[1]
      let nextTemp = item[2]
      console.log(item,preTemp,nextTemp,reg.test(newV))
      if(Number(preTemp) > Number(nextTemp) ) {
        this.$message({
          message: "前面温度不能大于后面温度!",
          type: "error",
        });
      }
      // 判断范围--2℃~40℃  如果为-5℃ 只获取到5
    
      if( (Number(nextTemp) < -2 ||  Number(nextTemp) > 41 ) || (Number(preTemp) < -2 ||  Number(preTemp) > 41 )) {
        this.$message({
          message: "输入的最低温度不能小于-2℃,最高不能大于40℃",
          type: "error",
        });
      }
  }
  },
  // immediate: true,
  deep: true
}
回复
1个回答
avatar
test
2024-07-20
// 先正则匹配到温度,在做过规则校验
const reg = /-?(\d+)?-?(\d+)℃/
回复
likes
适合作为回答的
  • 经过验证的有效解决办法
  • 自己的经验指引,对解决问题有帮助
  • 遵循 Markdown 语法排版,代码语义正确
不该作为回答的
  • 询问内容细节或回复楼层
  • 与题目无关的内容
  • “赞”“顶”“同问”“看手册”“解决了没”等毫无意义的内容