form表單提交前如何驗證數據不為空
HTML 的form 提交之前如何驗證數值不為空?為此陽光網小編為大家推薦了form表單提交前驗證數據不為空的方法,希望能幫到大家。
form表單提交前驗證數據不為空的方法
今天因為要做文件上傳,所以不再使用Ajax上傳,所以遇到了,input數據可能為空的問題,
所以新增一個驗證,提交時檢查是否空。
其實非常簡單,很多人可能不知道form表單本身就有一個驗證機制,具體如下:
js函數checkinput:
function checkinput(){
var name = document.getElementById("name").value;
if(name == null || name == ''){ alert("用戶名不能為空");
return false; }
return true;
}
Html form 表單提交前驗證方法
1 <HTML> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 </head> 5 <BODY> 6 <form action="http://www.baidu.com" onsubmit="return toVaild()"> 7 <input type="text" id="ff"> 8 <input type="submit" id="submit" value ="提交"/> 9 </form> 10 </BODY> 11 <script language="javascript"> 12 function toVaild(){ 13 var val = document.getElementById("ff").value; 14 alert(val); 15 if(val == "可以提交"){ 16 alert("校驗成功,之后進行提交"); 17 return true; 18 } 19 else{ 20 alert("校驗失敗,不進行提交"); 21 return false; 22 } 23 } 24 </script> 25 </HTML>
上面的網頁中,只有在id="ff"的`輸入框中輸入“可以提交”,才進行表單提交;否則不提交。
猜你感興趣:
【form表單提交前如何驗證數據不為空】相關文章:
2.勿以善小而不為
3.驗證氣象諺語作文
6.空飛絮
7.空杯