日期验证Java脚本 [英] date validation java script

查看:79
本文介绍了日期验证Java脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道我的代码(日期验证)不起作用我是php的新手,请帮助我,在此先感谢


i dont know y my code(date validation ) is not working i am newe to php , please help me , thanking in advance


<pre lang="PHP"><pre lang="xml"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript">
<!-- Form Validation -->
function validate_form( )
{
var format = /^\d{2}\/\d{2}\/\d{4}$/;
var date1= document.form1.textfield5.value;
valid = true;
if ( document.form1.textfield.value == "" ) {
alert ( "Please enter your User Name" );
valid = false;
return valid;}
if ( document.form1.textfield2.value == "" ) {
alert ( "Please enter password" );
valid = false;
return valid;}
if ( document.form1.textfield3.value != document.form1.textfield2.value ) {
alert ( "password mismatch" );
valid = false;
return valid;}

if(document.form1.textfield5.value == "")
{ valid=false;
alert("Enter a valid date");
return valid;
}
if ((!format.test(date1) )|| (!checkDate(format.exec(document.form1.textfield5))))
{alert("Please enter a date in the format mm/dd/yyyy");
}
var month = parseInt(date1[1]);
var date= parseInt(date1[2]);
var year= parseInt(date1[3]);
if (month<1 || month>12 || (day<30 || day>31)) valid=false;

switch (month) {

case 4: case 6: case 9: case 11:

if (day > 30)
{ valid= false;

break;
}
case 2:
if((year%400 ==0) || (year%100 != 0 && year%4 == 0) and (date>29))
{ valid= false;
break;
}
else
if(date>28)
{ valid= false;
break;
}
default:

if (day > 31)
{
valid=false;
break;
}
}
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var yyyy = today.getFullYear();

if (year<1 || year>yyyy) valid= false;

return valid;
}

return valid;

}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<blockquote>
  <blockquote>
    <blockquote>
      <blockquote>
        <p>&nbsp;</p>
        <blockquote>
          <p>&nbsp;</p>
          <form id="form1" name="form1" method="post" enctype="multipart/form-data" action="test.php"  onsubmit="return validate_form( );" >
            <blockquote>
              <table width="200" height="113" border="1">
                <tr>
                  <td colspan="2">Username</td>
                  <td width="76"><input type="text" name="textfield" /></td>
                </tr>
                <tr>
                  <td colspan="2">Password</td>
                  <td><input type="text" name="textfield2" /></td>
                </tr>
                <tr>
                  <td colspan="2">Retype password </td>
                  <td><input type="text" name="textfield3" /></td>
                </tr>
                <tr>
                  <td colspan="2">email id </td>
                  <td><input type="text" name="textfield4" /></td>
                </tr>
                <tr>
                  <td colspan="2">dob(mm/dd/yyyy)</td>
                  <td><input type="text" name="textfield5" /></td>
                </tr>
              </table>
              <blockquote>
                <blockquote>
                  <p align="justify">
                    <input type="submit" name="Submit" value="Submit" />
                  </p>
                </blockquote>
              </blockquote>
            </blockquote>
          </form>
        </blockquote>
      </blockquote>
    </blockquote>
  </blockquote>
</blockquote>
</body>
</html>

推荐答案

/; var date1 = 文档 .form1.textfield5.value; 有效= true ; 如果(文档 .form1.textfield.value == " "){ 警报(" ); 有效= false ; 返回有效;} 如果(文档 .form1.textfield2.value == " "){ 警报(" ); 有效= false ; 返回有效;} 如果(文档 .form1.textfield3.value!= 文档 .form1.textfield2.value){ 警报(" ); 有效= false ; 返回有效;} 如果(文档 .form1.textfield5.value == " ") {valid = false ; alert(" ); 返回有效; } 如果((!format.test(date1))||(!checkDate(format.exec(文档 .form1.textfield5)))) {alert(" ); } var month = parseInt (date1 [ 1 ]); var date = parseInt (date1 [ 2 ]); var year = parseInt (date1 [ 3 ]); 如果(月< 1 ||月> 12 ||(天< 30 ||天> 31))有效= false ; 开关(月){ 案例 4 :> case 6 :案例 9 : 11 : 如果(天> 30 ) {valid = false ; break ; } 案例 2 : 如果((year%400 == 0 )||(year%100!= 0 &&year;%4 == 0 )和(date> 29)) {valid = false ; break ; } 其他 如果(日期> 28) {valid = false ; break ; } 默认: 如果(天> 31 ) { valid = false ; break ; } } 今天 var = 日期(); var dd = today.getDate(); var mm = today.getMonth()+ 1; var yyyy = today.getFullYear(); 如果(year< 1 || year> yyyy)有效= false ; 返回有效; } 返回有效; } < / 脚本 > < 头部 > < =" Content-Type" 内容 =" text/html; charset = iso-8859-1" / > < 标题 > 无标题文档< /title > < /head > < 正文 > < blockquote > < blockquote > < blockquote > < blockquote > < p > & nbsp; < /p > < blockquote > < p > & nbsp; < /p > < 表单 =" form1" 名称 form1" 方法 =" 发布" 编码 multipart/form-data" 动作 test.php" =" 返回validate_form();" > < blockquote > < =" 200" 高度 > 113" 边框 =" 1" < tr > < td =" 2" < > < td =" 76" < 类型 =" 名称 =" / < /td > < /tr > < tr > < td =" 2" < > < td > < 输入 =" 文本" 名称 =" / > < > < /tr > < tr > < td =" 2" < > < td > < 输入 =" 文本" 名称 =" / > < > < /tr > < tr > < td =" 2" < > < td > < 输入 =" 文本" 名称 =" / > < > < /tr > < tr > < td =" 2" < > < td > < 输入 =" 文本" 名称 =" / > < > < /tr > < /table > < blockquote > < blockquote > < p =" justify" < 输入 =" 提交" 名称 提交" =" 提交" / < /p > < /blockquote > < /blockquote > < /blockquote > < /form > < /blockquote > < /blockquote > < /blockquote > < /blockquote > < /blockquote > < /body > < /html >
/; var date1= document.form1.textfield5.value; valid = true; if ( document.form1.textfield.value == "" ) { alert ( "Please enter your User Name" ); valid = false; return valid;} if ( document.form1.textfield2.value == "" ) { alert ( "Please enter password" ); valid = false; return valid;} if ( document.form1.textfield3.value != document.form1.textfield2.value ) { alert ( "password mismatch" ); valid = false; return valid;} if(document.form1.textfield5.value == "") { valid=false; alert("Enter a valid date"); return valid; } if ((!format.test(date1) )|| (!checkDate(format.exec(document.form1.textfield5)))) {alert("Please enter a date in the format mm/dd/yyyy"); } var month = parseInt(date1[1]); var date= parseInt(date1[2]); var year= parseInt(date1[3]); if (month<1 || month>12 || (day<30 || day>31)) valid=false; switch (month) { case 4: case 6: case 9: case 11: if (day > 30) { valid= false; break; } case 2: if((year%400 ==0) || (year%100 != 0 && year%4 == 0) and (date>29)) { valid= false; break; } else if(date>28) { valid= false; break; } default: if (day > 31) { valid=false; break; } } var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; var yyyy = today.getFullYear(); if (year<1 || year>yyyy) valid= false; return valid; } return valid; } </script> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> <blockquote> <blockquote> <blockquote> <blockquote> <p>&nbsp;</p> <blockquote> <p>&nbsp;</p> <form id="form1" name="form1" method="post" enctype="multipart/form-data" action="test.php" onsubmit="return validate_form( );" > <blockquote> <table width="200" height="113" border="1"> <tr> <td colspan="2">Username</td> <td width="76"><input type="text" name="textfield" /></td> </tr> <tr> <td colspan="2">Password</td> <td><input type="text" name="textfield2" /></td> </tr> <tr> <td colspan="2">Retype password </td> <td><input type="text" name="textfield3" /></td> </tr> <tr> <td colspan="2">email id </td> <td><input type="text" name="textfield4" /></td> </tr> <tr> <td colspan="2">dob(mm/dd/yyyy)</td> <td><input type="text" name="textfield5" /></td> </tr> </table> <blockquote> <blockquote> <p align="justify"> <input type="submit" name="Submit" value="Submit" /> </p> </blockquote> </blockquote> </blockquote> </form> </blockquote> </blockquote> </blockquote> </blockquote> </blockquote> </body> </html>


var date1 = document.form1.textfield5.value;
var month = parseInt(date1 [1]);

第二行没有意义. date1是日期字符串值.您如何将其假定为数组,并如何使用索引从中获取月,日或年.如果要使用此数组,请首先使用分隔符将date1拆分为数组.

试试:
var date1= document.form1.textfield5.value;
var month = parseInt(date1[1]);

Second line does not make sense. date1 is a date string value. How are you assuming it as an array and using an index to get month,date or year from it. If you want to use this array thing then split the date1 into an array first using their separator.

Try:
var dateBroken = date1.split("/");
//Now use this
// assuming it was mm/dd/yyyy
var month = dateBroken[0];
var day = dateBroken[1];
var year = dateBroken[2];


sandeep@ thanks buddy , but even after changing the code to
sandeep@ thanks buddy , but even after changing the code to
var dateBroken = date1.split("/");
//Now use this
// assuming it was mm/dd/yyyy
var month = dateBroken[0];
var day = dateBroken[1];
var year = dateBroken[2];

i get some error please get me relieved

i get some error please get me relieved


这篇关于日期验证Java脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆