<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>表单验证</title> <script type="text/javascript"> function checkForm(){ //表单验证 //01.获取用户的输入 var userName= document.getElementById("userName"); //02.验证 if(userName.value==""||userName.value.length<6){ alert("用户名不能为空!用户名长度不足!"); return false; } return true; } </script> </head> <body> <form action="#" method="post" onsubmit="return checkForm()"> <input type="text" name="userName" id="userName" placeholder="请输入用户名"> <input type="submit" value="提交"> </form> </body> </html>
相关文章: