var username = document.getElementById("username"); var username = document.getElementsByName("username")[0]; var nodes = document.getElementsByTagName("input");
HTML代码:
<form onsubmit="return validate()" action="loginServlet"> username:<input type="text" id="username" name="username"/><br /> password:<input type="password" id="username" name="password"><br /> <input type="submit" value="submit" /> </form>
JS验证代码:
function validate(){ //使用getElementById方法获取元素 var username = document.getElementById("username"); var password = document.getElementById("password"); //使用getElementsByName获取一组元素 //var username = document.getElementsByName("username")[0]; //var username = document.getElementsByName("password")[0]; //获取元素的值 并判断长度 if(username.value.length == 0){ alert("用户名不能为空"); return false; } if(password.value.length <6){ alert("密码不能少于6位"); return false; } return true; } //根据标签获取元素组 var nodes = document.getElementsByTagName("input"); for(var i=0; i<nodes.length; i++){ //获取标签类型 alert(nodes[i].type); }
HTML代码:
男<input type="radio" name="gender" value="男"> 女<input type="radio" name="gender" value="女">
JS验证代码:
//单选按钮的验证 var gender = document.getElementsByName("gender"); if(!gender[0].checked && !gender[1].checked){ alert("请选择性别!"); }
HTML代码:
<input type="checkbox" name="selectAll" onclick="selectAll()">Select all<br /> <input type="checkbox" name="number" value="1">1<br /> <input type="checkbox" name="number" value="2">2<br /> <input type="checkbox" name="number" value="3">3<br /> <input type="checkbox" name="number" value="4">4<br /> <input type="checkbox" name="number" value="5">5<br /> <input type="checkbox" name="number" value="6">6<br /> <input type="checkbox" name="number" value="7">7<br /> <input type="checkbox" name="number" value="8">8<br /> <input type="checkbox" name="number" value="9">9<br /> <input type="checkbox" name="number" value="10">10<br /><input type="checkbox" name="number">10<br />
JS代码:
/* 复选框全选的函数 */ function selectAll(){ var selectAll = document.getElementsByName("selectAll")[0]; if(selectAll.checked){ var numbers = document.getElementsByName("number"); for(var i=0; i<numbers.length; i++){ numbers[i].checked = true; } } else { for(var i=0; i<nmubers.length; i++){ numbers[i].checked = false; } } }
判断复选框是否有选择:
/* 验证用户选择的个数 */ function validateCheckbox(){ var n = 0; for(var i=0; i<numbers.length; i++){ if(numbers[i].checked){ n++; } } if(n < 1){ alert("至少要选择一项"); } }
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); //获取单选按钮的值 String gender = req.getParameter("gender"); //获取复选框的值 String[] numbers = req.getParameterValues("number"); List<String> list = new ArrayList<String>(); //验证判断 if("".equals(username)){ list.add("用户名不能为空"); } if(password == null){ list.add("用户密码不能为空"); } if(password != null && password.length()<6){ list.add("用户密码不能少于6位"); } //页面跳转 if(list.isEmpty()){ req.getRequestDispatcher("index.jsp").forward(req, resp); } else { req.setAttribute("error", list); req.getRequestDispatcher("error.jsp").forward(req, resp); } }
req.setCharacterEncoding("utf-8"); resp.setCharacterEncoding("utf-8"); ... //编码设置 gender = new String(gender.getBytes("iso-8859-1"),"utf-8");