文本框获取焦点,清空信息
文本框失去焦点时,
如果为空 ,提示 不能为空,
如果为非数字,提示不合法,
如果长度小于6,提示“长度不够”
如果为数字,提示正确
<input type="text" name="" id="text" value="输入内容(数字)" />
<script>
let txt = document.getElementById('text');
//获取焦点
txt.onfocus = function() {
//判断内容是否变化
if (this.value === this.defaultValue) {
this.value = '';
}
}
//失去焦点
txt.onblur = function() {
if (this.value === '') {
alert("内容不能为空");
} else if (isNaN(this.value)) {
alert("输入内容不合法")
} else if (this.value.length < 6) {
alert("长度不够");
} else {
alert("正确");
}
}
</script>
</body>