javascript实现文本框标签验证的实例代码
作者:在世虞姬 时间:2024-04-25 13:07:41
具体代码如下所述:
< !DOCTYPE html > <html lang = "en" > <head > <meta charset = "UTF-8" > <title > Document < /title>
</head > <style type = "text/css" > body {
background: #ccc;
}
label {
width: 100px;
display: inline - block;
}
span {
color: red;
}.container {
margin: 100px auto;
width: 400px;
padding: 50px;
line - height: 40px;
}
span {
margin - left: 30px;
font - size: 12px;
} < /style>
<body>
<div class="container">
<label>姓名不能为空</label > <input type = "text"id = "inp1" > <span > </span><br/ > <label > phone不能为空 < /label><input type="text" id="inp2"><span></span > <br / ></div>
<script>
/ / 失去焦点后判断用户输入是否为空
var inp1 = document.getElementById("inp1");
inp1.onblur = function() {
if (trim(this.value) === "") {
alert("输入不能为空");
} else {
alert("输入正确");
}
};
function trim(str) {
return str.replace(/^\s+|\s+$/g, "");
}
var inp2 = document.getElementById("inp2");
inp2.onblur = function(e) {
if (inp2.value == "4444") {
alert("正确");
e.stopPropagation();
} else {
alert("错误")
}
}; < /script>
</body >
</html>
总结
以上所述是小编给大家介绍的javascript实现文本框标签验证的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
来源:https://www.cnblogs.com/lan-f/archive/2018/10/13/9781782.html
标签:js文本框,验证
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaScript实现Tab标签页切换的最简便方式(4种)
2024-04-17 10:30:36
![](https://img.aspxhome.com/file/2023/0/136260_0s.jpg)
用Popup窗口建无限级Web页菜单
2023-07-10 20:22:17
看ASP程序源码的方法及工具
2009-01-21 19:58:00
Python中的各种装饰器详解
2023-02-23 06:16:41
用 SQL 脚本将 Access 导入 MSSQL 2000/2005 方法
2008-10-22 13:51:00
pytorch Dataset,DataLoader产生自定义的训练数据案例
2022-12-05 06:41:23
浅析Go设计模式之Facade(外观)模式
2023-07-16 19:23:08
利用pandas进行大文件计数处理的方法
2021-05-20 02:36:55
基于YUV 数据格式详解及python实现方式
2021-12-03 07:28:59
![](https://img.aspxhome.com/file/2023/9/93859_0s.jpg)
js树插件zTree获取所有选中节点数据的方法
2024-05-02 16:20:55
php简单定时执行任务的实现方法
2023-08-19 21:34:09
深入理解Python爬虫代理池服务
2022-11-05 17:20:29
![](https://img.aspxhome.com/file/2023/9/100819_0s.png)
在Python中使用SQLite的简单教程
2022-05-12 04:01:46
javascript获取浏览器类型和版本的方法(js获取浏览器版本)
2024-06-07 15:51:32
pandas使用get_dummies进行one-hot编码的方法
2023-10-22 06:43:02
![](https://img.aspxhome.com/file/2023/1/107091_0s.jpg)
对python while循环和双重循环的实例详解
2021-01-17 22:22:59
使用Javascript面向对象的思想编写ASP
2008-06-16 12:20:00
python机器学习实战之树回归详解
2022-02-05 04:37:10
Django中自定义模型管理器(Manager)及方法
2022-12-01 17:53:05
python中的zip模块
2023-09-13 07:49:56