当表单提交时先触发验证的js代码,当验证表单的方法返回true时才会提交表单返回false则不提交数据
<script type="text/javascript">
function checkIP(){//js表单验证方法
var text=document.getElementById("url").value;//通过id获取需要验证的表单元素的值
if(text==""){//当上面获取的值为空时
alert("不能为空哦!");//弹出提示
return false;//返回false(不提交表单)
}
return true;//提交表单
}
</script>
<FORM METHOD=POST ACTION="" name="ipform" onsubmit="return checkIP();">
<!--使用onsubmit方法进行表单验证-->
<input type="text" id="url" name="ip" size="15" maxlength="50" value="" />
<input type="submit" name="button" value="查询" /></FORM>
定义和用法
onsubmit 事件会在表单中的确认按钮(type="submit")被点击时发生。
dopost方法部分修改
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAC6CAIAAAD9F6QrAAAdnklEQVR4nO2dP2/kutWHlQ+QOt/BdWAk+hBbXZcp3Kh24WYrA6ncXU3hcts0NjBV1BruAsOAOzdT3MrAAtvd9gXMt9A//jmUqBmNJErPA2Lh1VAkRWrObw5F8SQKAABgwySz1XzYPyRXD/mnefTzJb26S67u3I+KnXDwHHRUVOzukqvHIqCQQ54mWUjGc1Bk89V9LEWWlETXcjgFxv14Dnma5oe5W7EOliaE/o9WJYTN1/9oE+CtwPigyJL2q6J/Ip4erN6HPD2t9R3lBpTnZtP7cxbbMOsPHxfGfRsUGb8gxiEeIVwCYwqhbpqO+A57KrAOn8MgHtngEE4xiFWTzti6YU2aFcZ9KxgjDUczthAe9g/J1WO2K6c377LX5mAlbI2WlAez3YOeUytEE0LffGl7/C65fZHvhjLP7kP/b7r/VbfELNZTkZazRwj1n8yG8bEO6ZOX2tdbyGkcLG95oxLzt7D9tZAMoni6p8wiS7K8/qjH5EiNN85PssI522ywY+mcMv3trIuxzX5Xf9aHrd8loaf3DoeL2yFD2hkM4x4wcPJlyqf35+w83TtKR9wM1hgs7CdYrJxFCCvhsTRPEkI7p1aI7RHK6tgonB+98FKnfU0SK+rI6dTUTlQ0t6duANq/NctbZNW9Lef0PfAT73/n56ExBWt/2wI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xJ85l6XJY6NWa6+6Btpj0VVf6fIy0+Kx9UeylLT+rmUi5Zb4a2rsRuYdWA96/9fTXr+Havbu6/3mrFKhvftPPtXt1cq/37177WeMu17X0ua/XG2331d/m74XghHG92FCEkkaZPCOGZU/1gacAp4rM356DvCZa3GaYceq18QO2lS1c+53MnTneiGjnN292rxlvda87i7qn2Duuqd0/tzOfN09fNZftp+QSx/Fd/4uhLb/fq2yVCSCKRjIQQnj3dSPOfP//0To3+9MxnWgdd181bUZne1TdthlP0/AJrv7lU35LqOd+3+8oFrN07TcOkBkgeYfV87uJa7e6/fj6pb9fGwpnd09fbU/uM0Ljk66+fjQ96aVbtdq+22qWdGh1FCE+eGv37/aH8L0JIIk2fEMLzJ10AdK/IL4Sye2cddBeqSBXpE5W+qVR7tra79qevvalJugvYPP/zpWapy86RnLLMcv6zbdK7+qYt89GF8O1e7Z/UxbXaXQuVWtOqZW/rHVU+FBSF0Jrd7ZXDkxbLvP/+96R9rQIhJJGmTwjhQtPCX643lp5qM41BzXuvHMrqv09q9/61v1bfLtvnfG1d9RPBn5YQNot0ntTuyS/qU6QT50UP/77EIySR5kwIIYl0Ujrlhfr/3f9Tnxf9iRCSSHMkhJBEOiE9ZcJmMeEHnYQQkkjTJ+IRAgDApkEIAQBg0yCEAACwaRBCAADYNAghAABsGoQQAAA2DUIIAACbBiEEAIBN8/8ijBwCQQakzQAAAABJRU5ErkJggg==" alt="" />
req.setCharacterEncoding("utf-8");
res.setContentType("text/html;charset=utf-8");
必须放在dopost()方法的第一行
在添加charset=utf-8到res.setContentType中