jQuery/JavaScript日期表单验证 [英] jQuery/JavaScript Date form validation

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

问题描述

我正在以某种形式使用jQuery日期选择器日历.提交后,表单会将参数通过url传递到第三方站点.一切正常,除了一件事. 如果随后删除了由日期选择器日历插入日期字段的值,或者删除了页面加载表单中的默认日期并提交了表单,则会出现以下错误:

I am using the jQuery date picker calendar in a form. Once submitted the form passes params along via the url to a third party site. Everything works fine, except for one thing. If the value inserted into the date field by the datepicker calendar is subsequently deleted, or if the default date, that is in the form on page load, is deleted and the form is submitted I get the following error:

从字符串"转换为日期"类型无效."

"Conversion from string "" to type 'Date' is not valid."

解决我的问题的方法非常简单,我想验证提交日期的文本字段,如果由于某种原因该字段为空,则发送默认日期(当前日期).问题是我在Java语言方面很糟糕,无法弄清楚该怎么做.

The solution to my problem is really simple, I want to validate the text field where the date is submitted and send out a default date (current date) if the field is empty for any reason. The problem is I am terrible at Javascript and cannot figure out how to do this.

这是我的日期字段的表单代码.

Here is the form code for my date field.

[var('default_date' = date)]
<input type="text" id="datepicker" name="txtdate" value="[$default_date]" onfocus="if (this.value == '[$default_date]') this.value = '';" onchange="form.BeginDate.value = this.value; form.EndDate.value = this.value;" />
            <input type="hidden" name="BeginDate" value="[$default_date]"/>
            <input type="hidden" name="EndDate" value="[$default_date]"/>

推荐答案

这确实很老,可能已经解决了,但是太麻烦了.

This is really old and probably solved, but what the heck.

最简单的方法是在输入的onchange事件中添加更多的javascript.

Simplest way to do this is to add a little more javascript to the input's onchange event.

onchange="if( this.value.length > 0 ) { form.BeginDate.value = form.EndDate.value = this.value; } else { form.BeginDate.value = form.EndDate.value = '[$default_date]'; } " />

当然,它们仍然是与日期验证相关的各种其他问题,但这是检查空值并根据您当前的代码结构返回默认值的直接方法.

of course their are still all sorts of other problems associated with date validation, but this is a straightforward way to check for a blank value and return a default based on your current code structure.

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

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