最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

validation - Validate Date input with Javascript - Stack Overflow

programmeradmin3浏览0评论

I have a input field date in my form and would like to add Javascript validation to ensure that its always greater than today's date. I tried the following but the function doesnt seem to be called when the user clicks on the input field and makes a change.

<input type="text" name="InsertRecordGuestOrders_Delivery_date" id="InsertRecordGuestOrders_Delivery_date" value=""/>

<script type="text/javascript">

function validateDate()
{
var del_date = document.getElementById('InsertRecordGuestOrders_Delivery_date').value;

if (Date.parse(del_date) < Date.now()) {
    document.getElementById('InsertRecordGuestOrders_Delivery_date').value = '';
    alert("Delivery date has to be later than today");
    }
}

document.getElementById('InsertRecordGuestOrders_Delivery_date').onChange = validateDate();

</script>

Any suggestions on what I'm doing wrong?

Thanks in advance.

I have a input field date in my form and would like to add Javascript validation to ensure that its always greater than today's date. I tried the following but the function doesnt seem to be called when the user clicks on the input field and makes a change.

<input type="text" name="InsertRecordGuestOrders_Delivery_date" id="InsertRecordGuestOrders_Delivery_date" value=""/>

<script type="text/javascript">

function validateDate()
{
var del_date = document.getElementById('InsertRecordGuestOrders_Delivery_date').value;

if (Date.parse(del_date) < Date.now()) {
    document.getElementById('InsertRecordGuestOrders_Delivery_date').value = '';
    alert("Delivery date has to be later than today");
    }
}

document.getElementById('InsertRecordGuestOrders_Delivery_date').onChange = validateDate();

</script>

Any suggestions on what I'm doing wrong?

Thanks in advance.

Share Improve this question asked Apr 3, 2013 at 16:55 MaverickMaverick 613 silver badges10 bronze badges 3
  • use document.getElementById('...').onChange = validateDate; – Tamil Selvan C Commented Apr 3, 2013 at 16:58
  • Tamil -Thanks for your quick response! I appreciate it. – Maverick Commented Apr 3, 2013 at 17:00
  • Possible duplicate of How to validate date with format "mm/dd/yyyy" in JavaScript? – Zorgatone Commented Dec 20, 2015 at 17:12
Add a ment  | 

2 Answers 2

Reset to default 5

You want to assign the validateDate function to the onchange property, not execute the function and assign its return value. Change this:

document.getElementById('...').onChange = validateDate();

to this:

document.getElementById('...').onChange = validateDate;

This line:

document.getElementById('InsertRecordGuestOrders_Delivery_date').onChange = validateDate();

Should be:

document.getElementById('InsertRecordGuestOrders_Delivery_date').onchange = validateDate;

Notice the parentheses are gone. If you invoke it immediately, you're assigning the return value of validateDate() as the onchange handler. You want to assign the function itself as the handler, not the return value of the function.

In addition, it should be onchange, not onChange.

发布评论

评论列表(0)

  1. 暂无评论