javascript實現(xiàn)文本框標簽驗證的實例代碼
更新時間:2018年10月14日 09:10:48 作者:在世虞姬
這篇文章主要介紹了javascript實現(xiàn)文本框標簽驗證的實例代碼,需要的朋友可以參考下
具體代碼如下所述:
< !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實現(xiàn)文本框標簽驗證的實例代碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
onkeydown事件解決按回車鍵直接提交數(shù)據(jù)的需求
登陸頁面需要撲捉用戶按下回車自動提交的需求,于是相到在body里添加onkeydown事件跳javascript在提交表單,具體看下實現(xiàn)代碼,希望對你有所幫助2013-04-04
微信小程序wx.getUserInfo授權獲取用戶信息(頭像、昵稱)的實現(xiàn)
這篇文章主要介紹了微信小程序wx.getUserInfo授權獲取用戶信息(頭像、昵稱)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08

