1.定义一个状态和一个函数用于获取输入框的数据
let [shou, setshou] = useState('')
let ji = (e) => {
setshou(e.target.value)
}
2.在输入框中绑定事件,当鼠标离开时进行验证手机号
<div>手机号:<input onBlur={handleBlur} value={shou} onChange={(e) => ji(e)} placeholder='输入手机号' />
<p style={isValid?{display: 'none'}:{display: ''}}>手机号错误</p>
</div>
3.对手机号进行验证
const [isValid, setIsValid] = useState(true);
const handleBlur = () => {
const phoneRegex = /^1[3-9]\d{9}$/;
const isValidPhone = phoneRegex.test(shou);
setIsValid(isValidPhone);
};