在用戶提交信息的時候通常會輸入手機號之類的,所以需要前端在提交的時候驗證其輸入的格式是否正確,以是在uni-app項目中,僅展示驗證部分內容
手機號:
let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/ //正則表達式定義手機號正確格式
if (!this.tele) { //判斷如果手機號(this.tele)為空,提示用戶輸入手機號
uni.showToast({
title: '請輸入手機號',
icon: 'none'
})
return
}
if (!reg.test(this.tele)) { //判斷手機號格式時候正確
uni.showToast({
title: '請輸入正確的手機號',
icon: 'none'
})
return
}
身份證號:
let reg1 = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/[]() //正則表達式定義身份證號正確格式
if (!this.card_num) { //判斷如果身份證號(this.card)num)為空,提示用戶輸入身份證號
uni.showToast({
title: '請輸入身份證號',
icon: 'none'
})
return
}
if (!reg1.test(this.card_num)) { //判斷身份證號格式時候正確
uni.showToast({
title: '請輸入正確的身份證號',
icon: 'none'
})
return
}
