Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数字、日期和值的JSP表单验证_Java_Javascript_Eclipse_Jsp - Fatal编程技术网

Java 数字、日期和值的JSP表单验证

Java 数字、日期和值的JSP表单验证,java,javascript,eclipse,jsp,Java,Javascript,Eclipse,Jsp,您好,我有一个包含表单的JSP页面,我想验证表单数据,如出生日期格式和值、繁殖id-动物园id(仅为数字)和存在的动物名称 这是我的JSP页面: <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page import="java.util.ArrayList" %> <%@page import="content.*"%> <%@ page language="ja

您好,我有一个包含表单的JSP页面,我想验证表单数据,如出生日期格式和值、繁殖id-动物园id(仅为数字)和存在的动物名称

这是我的JSP页面:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ page import="java.util.ArrayList" %>
<%@page import="content.*"%>
<%@ page language="java" 
         contentType="text/html; charset=windows-1256"
         pageEncoding="windows-1256" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>



<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<title>Append Animal</title>
<link rel="stylesheet"
      href="./css/styles.css"
      type="text/css"/>
</head>
<body>
<table class="title">
  <tr><th>Zoo keeper</th></tr>
</table>


<h1>Append Animal</h1>

<form name="insert" action="Relay" >
<fieldset>
Animal new name: <input type= "text" name = "aname"><br>
Animal new DOB: <input type= "text" name = "dob"><br>


Animal new gender: 
<select name="gender" id="gender">
<option value="male">Male</option>
<option value="female">Female</option>
</select>
<br>
Animal status: 
<select name="source" id="source">
<option value="born">Born</option>
<option value="bought">Bought</option>
</select>
<br>
Animal old zoo: <input type= "text" name = "zooid" > only if bought<br>
Animal new Breed: <input type= "text" name = "breedid" ><br>
Animal new remarks: <textarea name = "remark" rows="4" cols="20">

</textarea> <br /> <br/>


<input type="submit" value="submit">

<input type="hidden" name="command" value="AppendAnimalServlet" > 

</fieldset>
</form>
</body></html>

附加动物
动物园管理员
附加动物
动物新名称:
动物新出生日期:
动物新性别: 男性 女性
动物状态: 天生的 买了
动物旧动物园:仅限购买
动物新品种:
动物新评论:

我在JSP页面中添加了此脚本:

<script>
function validateForm()
{
    if(document.insert.aname.value ==="")
    {
      alert("Animal should have a name");
      document.insert.aname.focus();
      return false;
    }
    if(document.insert.zooid.value !=="")
    {
        if (! (/^\d*(?:\.\d{0,2})?$/.test(document.insert.zooid.value))) { 
            alert("Please enter a valid Zoo id"); 
            document.insert.zooid.focus(); 
            return false; 
        } 

      } 
    if(document.insert.breedid.value !=="")
    {
        if (! (/^\d*(?:\.\d{0,2})?$/.test(document.insert.breedid.value))) { 
            alert("Please enter a valid Breed id"); 
            document.insert.breedid.focus(); 
            return false; 
        } 

      } 
    if(document.insert.dob.value ==="")
    {
      alert("Animal should have a date of birth");
      document.insert.aname.focus();
      return false;
    }

}
</script>

函数validateForm()
{
如果(document.insert.aname.value==“”)
{
警惕(“动物应该有名字”);
document.insert.aname.focus();
返回false;
}
如果(document.insert.zooid.value!==“”)
{
如果(!(/^\d*(?:\。\d{0,2})$/.test(document.insert.zooid.value)){
警报(“请输入有效的动物园id”);
document.insert.zooid.focus();
返回false;
} 
} 
if(document.insert.breedid.value!==“”)
{
如果(!(/^\d*(?:\。\d{0,2})$/.test(document.insert.breedid.value)){
警报(“请输入有效的品种id”);
document.insert.breedid.focus();
返回false;
} 
} 
如果(document.insert.dob.value==“”)
{
警惕(“动物应有出生日期”);
document.insert.aname.focus();
返回false;
}
}
但我认为我做得不对,有什么建议吗

请注意,表单值将被转换为中继servlet来处理插入过程

<form name="insert" action="Relay" onsubmit="return validateForm();">

如果不是家庭作业,请节省一些时间并使用:


可以使用一些通用代码构建验证库,如以下所示(只是一个简单的示例,它可以大大扩展):

函数验证(表单){
变量fnMap={
“验证日期ISO8601”:{
检查fn:isvalidos8601,
checkMsg:'日期必须采用yyyy mm dd格式'
},
“验证日期有效”:{
检查fn:isValidDate,
checkMsg:“日期必须是有效日期”
}
};
var reClass=/(^ |\s*)validate-[^\s]*/g;
var控件,控件=form.elements;
var检查,检查;
var fn,价值;

对于(var i=0,iLen=controls.length;i您需要向表单中添加一个sumbit侦听器,例如,
,如果验证未能停止提交,则从函数中返回false。还要注意,客户端验证只是为了方便用户,而且完全不可靠,您仍必须在服务器上进行验证。当我将其添加到表单中时,会出现以下错误:无法从函数或方法外部返回。什么时候会出现错误?当单击“提交”按钮时?当我单击“提交”按钮时,一切都正常,但我所说的这个错误存在于eclipse页面中,并用红色线下划线,另外一件事是,我想创建一些东西来检查我正在检查的日期,只要它不为空你知道检查日期吗?那么这是一个Eclipse问题,而不是javascript问题。添加一个“Eclipse”注意你的问题。至于验证日期,我相信这里有一百万篇关于这方面的文章。你可以使用一个简单的正则表达式来检查格式,或者更进一步,检查它是否确实是一个有效的日期。这两种方法都只需要几行代码。我担心这是一个家庭作业,但既然它起作用了,我想我只需要添加一个日期检查呃,接着说,它不会检查日期是否确实有效,只是检查它是否符合一个非常通用的模式。而且你需要在页面中添加大约6000行脚本库,才能完成可能需要3或4行代码的任务。
rules: {    
    dob: {
       required: true,
       date: true },    
    firstname: "required",
    breedid: "required"
}
function validate(form) {
  var fnMap = {

    'validate-dateISO8601': {
      checkFn: isValidISO8601,
      checkMsg: 'Date must be in format yyyy-mm-dd'
    },

    'validate-dateValid': {
      checkFn: isValidDate,
      checkMsg: 'Date must be a valid date'
    }
  };

  var reClass = /(^|\s*)validate-[^\s]*/g;
  var control, controls = form.elements;
  var check, checks;
  var fn, value;

  for (var i=0, iLen=controls.length; i<iLen; i++) {
    control = controls[i];

    // Need a function here to handle more control types
    value = control.value;
    checks = control.className.match(reClass);

    // If there are any validate- classes
    if (checks) {

      // For each validate class
      for (var j=0, jLen=checks.length; j<jLen; j++) {
        check = checks[j].replace(/\s/g,'');

        // See if there is a related function
        if (fnMap.hasOwnProperty(check)) {
          check = fnMap[check];

          // If there is, run it
          // If it fails, show the message
          // This part could be collected in an array and all the
          // error messages desplayed at once or in suitable message
          // elements in the document. But alert is cheap :-)
          if (!check.checkFn(value)) {
            alert(check.checkMsg);
            control.focus();
            return false;
          }
        }
      }
    }
  }
}

// Just checks the pattern is yyyy/mm/dd
function isValidISO8601(s) {
    s.replace(/^\s+|\s+$/g, '');
    return /\d{4}[-\/]\d{2}[-\/]\d{2}/.test(s);
} 

// Checks that s is a valid date
function isValidDate(s) {
    var bits = s.split(/[-\/]/g);
    var d = new Date(bits[0], bits[1] - 1, bits[2]);
    return d.getFullYear() == bits[0] && d.getDate() == bits[2];
}

</script>
<form onsubmit="return validate(this);">
  <input type="text" name="foo" class="validate-dateISO8601 validate-dateValid" value="2011-02-28">
  <input type="submit">
</form>