我正在使用javascript验证日期字段,我需要验证DD/MM/YY
如果用户输入日期为10/06/2012,则该日期有效
如果用户输入日期为3/06/2012,则表示我们需要在日期之前添加零作为03/06/2012.
我的javascript代码是
$('#date').blur(function () {
var collector = $('#date').val();
collector = collector.split("/");
if (collector[0].length != 2) {
if (collector[0].length == 1) {
//here code for make it as two digit
}
}
else
{
alert('Date is not entered properly');
}
});
Run Code Online (Sandbox Code Playgroud)
我的HTML
Date: <input type="text" id="date" name="date" />
Run Code Online (Sandbox Code Playgroud)
如何用javascript做到这一点
试试这个 :
collector[0] = "0" + collector[0];
Run Code Online (Sandbox Code Playgroud)
用你的代码:
$('#date').blur(function() {
var collector = $('#date').val();
collector = collector.split("/");
if (collector[0].length != 2) {
if (collector[0].length == 1) {
collector[0] = "0" + collector[0];
}
}
else {
alert('Date is not entered properly');
}
// set the val again
$('#date').val(collector.join('/'));
});?
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8918 次 |
| 最近记录: |