一個檢測表單數(shù)據(jù)的JavaScript實(shí)例
一個檢測表單數(shù)據(jù)的JavaScript實(shí)例,很簡單,很實(shí)用,感興趣的朋友可以看看
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>每天一個JavaScript實(shí)例-檢測表單數(shù)據(jù)</title>
<style>
[role="alert"]{
background-color: #fcc;
font-weight: bold;
padding:5px;
border:1px dashed #000;
}
div{
margin:10px 0;
padding:5px;
width:400px;
background-color: #fff;
}
</style>
<script>
window.onload = function(){
document.getElementById("thirdfield").onchange = validateField;
document.getElementById("firstfield").onblur = mandatoryField;
document.getElementById("testform").onsubmit = finalCheck;
}
function validateField(){
removeAlert();
if(!isNaN(parseFloat(this.value))){
resetField(this);
}else{
badField(this);
generateAlert("You entered an invalid value in Third Field. only numeric values such as 105 or 3.45 are allowed");
}
}
function removeAlert(){
var msg = document.getElementById("msg");
if(msg){
document.body.removeChild(msg);
}
}
function resetField(elem){
elem.parentNode.setAttribute("style","background-color:#fff");
var valid = elem.getAttribute("aria-invalid");
if(valid) elem.removeAttribute("aria-invalid");
}
function badField(elem){
elem.parentNode.setAttribute("style","background-color#fee");
elem.setAttribute("aria-invalid","true");
}
function generateAlert(txt){
var txtNd = document.createTextNode(txt);
msg = document.createElement("div");
msg.setAttribute("role","alert");
msg.setAttribute("id","msg");
msg.setAttribute("class","alert");
msg.appendChild(txtNd);
document.body.appendChild(msg);
}
function mandatoryField(){
removeAlert();
if(this.value.length > 0 ){
resetField(this);
}else{
badField(this);
generateAlert("You must enter a value into First Field");
}
}
function finalCheck(){
//console.log("aaa");
removeAlert();
var fields =document.querySelectorAll('input[aria-invalid="true"]');
//var fields =document.querySelectorAll("input[aria-invalid='true']");//錯誤!??!
console.log(fields);
if(fields.length > 0){
generateAlert("You have incorrect fields entries that must be fixed before you can submit this form");
return false;
}
}
</script>
</head>
<body>
<form id = "testform">
<div>
<label for="firstfield">*first Field:</label><br />
<input id="firstfield" name = "firstfield" type = "text" aria-required = "true" />
</div>
<div>
<label for="secondfield">Second Field:</label><br />
<input id="secondfield" name = "secondfield" type = "text" />
</div>
<div>
<label for="thirdfield">Third Field(numeric):</label><br />
<input id="thirdfield" name = "thirdfield" type = "text" />
</div>
<div>
<label for="fourthfield">Fourth Field:</label><br />
<input id="fourthfield" name = "fourthfield" type = "text" />
</div>
<input type="submit" value = "Send Data" />
</form>
</body>
</html>
相關(guān)文章
JavaScript中展開運(yùn)算符及應(yīng)用的實(shí)例代碼
這篇文章主要介紹了JavaScript中展開運(yùn)算符及應(yīng)用的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
基于javascript實(shí)現(xiàn)日歷功能原理及代碼實(shí)例
這篇文章主要介紹了基于javascript實(shí)現(xiàn)日歷效果原理及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-05-05
js實(shí)現(xiàn)視圖和數(shù)據(jù)雙向綁定的方法分析
這篇文章主要介紹了js實(shí)現(xiàn)視圖和數(shù)據(jù)雙向綁定的方法,結(jié)合實(shí)例形式分析了vue.js及jQuery數(shù)據(jù)綁定相關(guān)操作技巧與注意事項,需要的朋友可以參考下2020-02-02
JavaScript中的this例題實(shí)戰(zhàn)總結(jié)詳析
使用JavaScript開發(fā)的時候,很多人多多少少都會被this的指向問題搞蒙圈,下面這篇文章主要給大家介紹了關(guān)于JavaScript中this例題實(shí)戰(zhàn)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
NestJS使用class-validator進(jìn)行數(shù)據(jù)驗(yàn)證
本文將通過詳細(xì)的步驟和實(shí)戰(zhàn)技巧,帶大家掌握如何在NestJS中使用class-validator進(jìn)行數(shù)據(jù)驗(yàn)證,以及11條實(shí)戰(zhàn)中常用的驗(yàn)證技巧,感興趣的可以了解下2024-11-11
webpack-dev-server搭建本地服務(wù)器的實(shí)現(xiàn)
當(dāng)我們使用webpack打包時,發(fā)現(xiàn)每次更新了一點(diǎn)代碼,都需要重新打包,我們希望本地能搭建一個服務(wù)器,本文就介紹如何使用webpack-dev-server搭建本地服務(wù)器,感興趣的可以了解一下2021-07-07
JavaScript實(shí)現(xiàn)將xml轉(zhuǎn)換成html table表格的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)將xml轉(zhuǎn)換成html table表格的方法,實(shí)例分析了javascript操作XML文件與table表格的技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04

