javascript - 这是我的 Javascript 代码..如果 datepicker 为空,我需要添加对 datepicker 的验证,警报消息 "date can not be empty "

标签 javascript html

<html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <script type="text/javascript"> function
isDateSelected(){ 
    var today =new Date();
    var inputDate = new Date(document.myForm.date.value);
    if (inputDate.value == " "){
         return false;
     } else if (inputDate > today) {
         return false;
     } else {
         return true;
     } } </script> <style type="text/css"> .left {
      float: left;
      width: 200px;
      text-align: center;  }   .right {
      float: right;
      width: 100px;
      margin-top: 20px;  }   .center {
      margin: auto;
     width: 300px;  }

   .labelClass{
    float: left;
     width: 150px;
     margin-top: 20px;   }   .space{   padding: 0px 0px 0px 30px;}
      .divider{
     width:50px;
     height:auto;
     display:inline-block; } .button{ position:relative; left: 0%; margin-top:10%; }


 </style> </head> <body style="background-color:#D3D3D3"> <form
id="date" name="myForm" action="demo_form.asp" onsubmit="return
 isDateSelected();">

 <div> <h1 style="padding: 10px; color: black; background-color:
 #D3D3D3; border: #82CAFF 2px solid"><center>Derivative Trade Report Adhoc</center></h1> </div>

 <fieldset style="height: 250px;" style="margin-top:50px;">
 <legend>Input Details</legend>

 <table cellspacing="5" border="0" style="padding-left: 120px;">


 <tr>

 <td> <span class="labelClass"> From Date:</span>   <input type="date" 
 class="labelClass"  > </td>  <td class="space">Clients(For other than
 Derivative Cash)</td>    </tr>    <tr>  <td> <span class="labelClass">
 To Date:</span> <input type="date"  class="labelClass"> </td>

 <td class="space" rowspan="2">

 <select autofocus="true" multiple="multiple" style="width: 250px;">  
 <option value="All Clients">-ALL Clients-</option>   <option
 value="JHP-NAVLLP">JHP-NAVLLP</option>   <option
 value="JOHAMBRO">JOHAMBRO</option>   <option
 value="JOHCM">JOHCM</option>   <option value="L&C">L&C</option>  
 </select> </td> </tr> <tr> <td> <span class="labelClass"> TPA
 Name:</span>  <input type="text" class="labelClass"> </td> </tr> <tr>
 </tr> <tr> </tr> </table> </fieldset>

 <div style="padding: 20px;text-align:center;border: #82CAFF 2px
 solid;margin-top: 5%" class="button"  > <input type="submit" name="Run
Report" value="Create Report"></input> </div>

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

最佳答案

像这样改变你的函数:

    isDateSelected(){ 
        var today =new Date();
        var inputDate = new Date(document.myForm.date.value);
        if (inputDate.value == ""){
             alert("date cannot be empty");
             return false;
         } else if (inputDate > today) {
             return false;
         } else {
             return true;
         } 
    }

关于javascript - 这是我的 Javascript 代码..如果 datepicker 为空,我需要添加对 datepicker 的验证,警报消息 "date can not be empty ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36716898/

相关文章:

html - 如何让渐变在 bootstrap 和 css 中占据全屏?

html - 音频API:无法恢复音乐并对其进行可视化。 html5-audio中是否有bug?

javascript - 将参数从速度传递给 javascript

javascript - 如何在单页 javascript/html5 应用程序中设置 href

javascript - 在另一个函数内部调用函数

javascript - 为什么 JS 报告 'this.function' 不是函数?

html - 为什么悬停样式在应用于我的 div 时不起作用?

javascript - JSPDF 自定义字体添加不起作用

javascript - 如何在 Laravel Mix 中使用全局 JS 变量?

javascript - 如何制作一个从 02 :00 reaches 00:00 and then restarts at 06:00 开始的计时器