Notes on commonly used conditional restriction methods in the front end

Regular expression for mobile phone number (11 digits starting with 1)

function checkPhone(){ 
    var phone = document.getElementById('phone').value;
    if(!(/^1[3456789]\d{9}$/.test(phone))){ 
        alert("手机号码有误,请重填");  
        return false; 
    } 
}

Limit the input to be greater than 0 and the minimum value to be less than the maximum value

@change="onInputNumChange"
    // 数值改变(调整方式为减少,并且减少数量大于当前总数时,自动赋值总数)
    onInputNumChange(newVal) {
      if (this.ctrlType === 2 && newVal > this.cacheMaxNum) {
        this.ctrlNum = this.cacheMaxNum;
      }

The restriction can only select a date after today, and only within 6 months

// :picker-options="dateRange" :当前时间日期选择器特有的选项参考下表
// 通过设置picker-options来达到限制可选择的时间范围
<el-date-picker v-model="time" type="daterange" range-separator="至"
    start-placeholder="选择开始日期" end-placeholder="选择结束日期" :clearable="false"
    :picker-options="dateRange">
</el-date-picker>
// vue中的data中定义
data() {
	return {
	  // 日期选择器可选择范围
      dateRange: {
        disabledDate(time) {
          // 限制最早只能从5月份开始查询
          // 当前日期小于2021-04-30禁止选择
          return new Date(time).getTime() < new Date('2021-04-30').getTime();
          // 也可以设置最大可选择日期
          // 当前日期小于2021-04-30或者大于2021-05-31都禁止选择
          // return new Date(time).getTime() < new Date('2021-04-30').getTime() || new Date(time).getTime() > new Date('2021-05-31').getTime();
        },
      },
	}
}

or

<el-date-picker class="filter-item" type="daterange"  value-format="yyyy-MM-dd" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions">
</el-date-picker>
pickerOptions: {
          onPick: ({ maxDate, minDate }) => {
            this.choiceDate = minDate.getTime()
            if (maxDate) {
              this.choiceDate = ''
            }
          },
          disabledDate: (time) => {
            if (!isNull(this.choiceDate)) {
              const one = 30 * 24 * 3600 * 1000
              const minTime = this.choiceDate - one
              const maxTime = this.choiceDate + one
              return time.getTime() < minTime || time.getTime() > maxTime
            }
          }
},
choiceDate: ''

Guess you like

Origin blog.csdn.net/Vivien_CC/article/details/128775395