2016-06-18 7 views
0

私は以下のJavascriptコードを持っています。これにより、日付が空にならないようにすることができますが、クライアントが今日より前の日を選択するのを防ぐためにも必要です。それについてどうすればいいですか?JavaScriptを使用したクライアント側の確認

HTML

<form name="myForm" method="post" action="http://www.randyconnolly.com/tests/process.php" onsubmit="return submitFunction()"> 

<table border="1" > 
<tr> 
<td>Date:</td> 
<td><input type="date" name="date" id="date"></td> 
</tr> 
<tr> 
<td><input type="Submit" value="Submit"></td><td>Click Submit to validate this form!</td> 
</tr> 
</table> 

Javascriptを

function submitFunction(myForm) 
{ 
var errormessage =""; // Generates Error Message if true 

/*Manages a control that allows the errormessage variable to determine* 
whether or not it has been deemed true. If true, an error message will appaear 
If false, the code will continue to move along and validate the data. */ 

if (document.getElementById('date').value=="") 
{ 
errormessage+="Select a valid date \n"; 
document.getElementById('date').style.borderColor="red"; 
} 
else { 
    document.getElementById('date').style.borderColor="green"; 
} 

if(errormessage!="") 
{ 
alert(errormessage);   
return false; 
} 


} 

答えて

0

私はあなたの検証要件

HTML

<input type="date"> 
<button>Check</button> 

を実装JSFiddleを作成JavaScript

var dateElem = document.querySelector('input'), 
    todayDate = new Date(); 

todayDate.setHours(0); 
todayDate.setMinutes(0); 

document.querySelector('button').onclick = function() { 
    if(dateElem.value === '') { 
    alert('Please choose a date!'); 
    } else { 
    if(todayDate.getTime() > (new Date(dateElem.value)).getTime()) { 
     alert('You can not choose a date prior to today!'); 
    } else { 
     alert('Valid date :)'); 
    } 
    } 
} 
+0

JSFiddleでチェックしても機能しましたが、コードでは機能しませんでした。たぶん私はあなたにHTMLの部分を提供すれば助けになるでしょう。HTMLは以下の通りです: ​​​​​​このフォームを検証するには[Submit]をクリックしてください! – Kaky

+0

@Kakyあなたの 'html'にマッチするようにJSFiddleを更新しました。 –

+0

が動作しない:( – Kaky

関連する問題