如何使用java脚本验证Form
How to validate Form using java Scripts
我正在尝试使用java脚本验证表单,但它不起作用。
这是我的代码,它不能正常工作。
我已经试过很多次了,但它并没有给我任何警告,即使我给出了错误的值。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%ResultSet resultset =null;%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type= "text/javascript" src = "countries.jsp"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Form</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css"
href="css/bootstrap-responsive.css">
<link rel="stylesheet" type="text/css"
href="css/bootstrap-responsive.min.css">
<script type="text/javascript">
function validate()
{
if(form.firstname.length==1) {
alert("Error: Input is empty!");
form.firstname.focus();
return false;
}
}
</script>
</head>
<body>
<h1 align="center">
<b>REGISTRATION FORM</b>
</h1>
<form action="welcome.jsp" method="post" onsubmit="return validate()">
<div align="center">
<label for="inputFirstname">First Name
</label>
<div>
<input type="text" name="firstname"
placeholder="firstname" ></input>
</div>
</div>
<div align="center">
<label for="inputLastname">Last Name </label>
<div>
<input type="text" name="lastname" maxlength="12"
placeholder="lastname"></input>
</div>
</div>
<div align="center">
<label for="inputMobile">Mobile</label>
<div>
<input type="text" name="mobile" maxlength="10"
placeholder="Ex:95xxxx4104"></input>
</div>
</div>
<div align="center">
<div>
Select Country:<select id="country" name ="country">
<option value="volvo">India</option>
<option value="saab">Pakistan</option>
<option value="mercedes">Austrelia</option>
<option value="audi">Audi</option>
</select>
</div>
<div>
Select State:<select name ="state" id ="state"></select>
</div>
<div>
</div>
<div align="center">
<label for="inputEmail">E-mail</label>
<div>
<input type="text" name="email" maxlength="50"
placeholder="rakesh@gmail.com"></input>
</div>
</div>
<div align="center">
<label for="gender">Gender</label>
<div>
<input type="radio" name="optionsRadios" id="optionsRadios1"
value="Male" checked>Male <input type="radio"
name="optionsRadios" id="optionsRadios2" value="Female">Female
</div>
</div>
<div align="center">
<label for="inputAddress">Address </label>
<div>
<input type="text" name="address" maxlength="70" id="address"
placeholder="Address"></input>
</div>
</div>
<div align="center">
<label for="inputCity">City </label>
<div>
<input type="text" name="city" id="city" placeholder="Noida"></input>
</div>
</div>
<div align="center">
<label for="inputState">State </label>
<div>
<input type="text" id="state" name="state" id="state"
placeholder="Delhi"></input>
</div>
</div>
<div align="center">
<div>
<input type="reset" value="Reset" />
<input type="submit" value="Submit" onclick="validate()"/>
</div>
</div> </form>
</body>
</html>
</body>
</html>
请帮帮我。
尝试这个
对我来说,它在实时页面中工作
<html>
<head>
<title>Form Validation</title>
<script type="text/javascript">
function validate() {
if (document.myForm.Name.value == "") {
alert("Please provide your name!");
document.myForm.Name.focus();
return false;
}
if (document.myForm.EMail.value == "") {
alert("Please provide your Email!");
document.myForm.EMail.focus();
return false;
}
if (document.myForm.Zip.value == "" ||
isNaN(document.myForm.Zip.value) ||
document.myForm.Zip.value.length != 5) {
alert("Please provide a zip in the format #####.");
document.myForm.Zip.focus();
return false;
}
if (document.myForm.Country.value == "-1") {
alert("Please provide your country!");
return false;
}
return (true);
}
</script>
</head>
<body>
<form name="myForm" onsubmit="return(validate());">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td align="right">Name</td>
<td>
<input type="text" name="Name" />
</td>
</tr>
<tr>
<td align="right">EMail</td>
<td>
<input type="text" name="EMail" />
</td>
</tr>
<tr>
<td align="right">Zip Code</td>
<td>
<input type="text" name="Zip" />
</td>
</tr>
<tr>
<td align="right">Country</td>
<td>
<select name="Country">
<option value="-1" selected>[choose yours]</option>
<option value="1">USA</option>
<option value="2">UK</option>
<option value="3">INDIA</option>
</select>
</td>
</tr>
<tr>
<td align="right"></td>
<td>
<input type="submit" value="Submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
首先,代码中存在不正确的封闭标记你不需要关闭input
标签,如果你想这样做,那么
而不是这个
<input type="text" id="state" name="state" id="state" placeholder="Delhi"></input>
做这个
<input type="text" id="state" name="state" placeholder="Delhi" />
此外,您使用的是双id attributes
,而不仅仅是输入标签,您还有更多重复的标签。
对于JavaScript验证,您可以执行类似的操作
<script type="text/javascript">
function validate()
{
var inputTags = document.getElementsByTagName("input");
console.log(inputTags.length);
for(i=0;i < inputTags.length;i++){
console.log(inputTags[i].value);
if(inputTags[i].value === ""){
alert("All fields are required");
return false;
}
}
return true;
}
</script>
但它对你的代码不起作用,因为你所有的标签都是无效的格式,所以我从@davmszd答案中提取了示例代码并应用于此,它起到了的作用
检查
function validate()
{
var inputTags = document.getElementsByTagName("input");
console.log(inputTags.length);
for(i=0;i < inputTags.length;i++){
console.log(inputTags[i].value);
if(inputTags[i].value === ""){
//alert("All fields are required");
document.getElementById("status").innerHTML = "All fields are required";
return false;
}
}
return true;
}
<body>
<form name="myForm" onsubmit="return(validate());">
<table cellspacing="2" cellpadding="2" border="1">
<tr>
<td align="right">Name</td>
<td>
<input type="text" name="Name" />
</td>
</tr>
<tr>
<td align="right">EMail</td>
<td>
<input type="text" name="EMail" />
</td>
</tr>
<tr>
<td align="right">Zip Code</td>
<td>
<input type="text" name="Zip" />
</td>
</tr>
<tr>
<td align="right">Country</td>
<td>
<select name="Country">
<option value="-1" selected>[choose yours]</option>
<option value="1">USA</option>
<option value="2">UK</option>
<option value="3">INDIA</option>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Submit" />
</td>
<td align="right" id="status"></td>
</tr>
</table>
</form>
</body>
我使用的document.getElementsByTagName("input");
还有一件事,它应该总是在创建输入标签后使用。如果你在输入标签之前使用它,我将无法使用
相关文章:
- Angular Form Wide验证(不特定于字段)
- Form获胜'没有ID主联系表格无法验证
- Jquery验证插件在与<发件人:form>标签
- 如何使用组件和自定义验证实现Angular 1 Form
- javascript form validaton, 验证消息 快速消失.
- jQuery 验证 未捕获的类型错误 无法读取未定义的属性“form”
- 如何在 Plone 上禁用 z3c.form 的内联验证
- 阻止在 form.reset() 上验证
- jQuery Form Validator - 如何使用 javascript 更改数据验证可选属性
- Angular Form验证以显示错误消息
- 使用form.submit验证复选框部分
- 如何使用java脚本验证Form
- ASP.NET MVC Still Submit Form中的无干扰客户端验证
- 如何验证ng-form是否被触摸
- jQuery验证:$.data($('form')[0],'验证器').设置返回未定义
- Groovy & lt; g: form>使用jquery Validate()进行验证
- Jquery + form:如果val为空则不验证
- 使用Javascript提交HTML5 Form并验证其输入
- jQuery表单验证- https://github.com/victorjonsson/jQuery-Form-Val
- Javascript Form Validation赢得't验证