我有一個文本框和小鍵盤設計,用於生成用戶出生日期。允許以mm/dd/yyyy格式顯示月份,開始時使用javascript
HTML代碼
Memory = "0"; // initialise memory variable
Current = "0"; // and value of Display ("current" value)
Operation = 0; // Records code for eg */etc.
MAXLENGTH = 8; // maximum number of digits before decimal!
function format(input, format, sep) {
var output = "";
var idx = 0;
for (var i = 0; i < format.length && idx < input.length; i++) {
output += input.substr(idx, format[i]);
if (idx + format[i] < input.length) output += sep;
idx += format[i];
}
output += input.substr(idx);
return output;
}
function AddDigit(dig) //ADD A DIGIT TO DISPLAY (keep as 'Current')
{ if (Current.indexOf("!") == -1) //if not already an error
{ if ( (eval(Current) == 0)
&& (Current.indexOf(".") == -1)
) { Current = dig;
} else
{ Current = Current + dig;
};
Current = Current.toLowerCase(); //FORCE LOWER CASE
} else
{ Current = "Hint! Press 'Clear'"; //Help out, if error present.
};
\t
\t
if (Current.length > 0) {
\t Current = Current.replace(/\D/g, "");
Current = format(Current, [2, 2, 4], "/");
}
document.calc.display.value = Current.substring(0, 10);
}
function Clear() //CLEAR ENTRY
{ Current = "0";
document.calc.display.value = Current;
}
<form Name="calc" method="post">
<input class="intxt1" autocomplete="off" id="ptdob" maxlength="6" name="display" type="tel" value="" placeholder="MM/DD/YYYY"><button class="cancel-icon" type="reset" OnClick="Clear()"></button>
<div class="calculator" style="margin: 30px auto;">
\t <!-- Screen and clear key -->
\t <div class="keys">
\t \t <!-- operators and other keys -->
\t \t <span OnClick="AddDigit('1')">1</span>
\t \t <span OnClick="AddDigit('2')">2</span>
\t \t <span OnClick="AddDigit('3')">3</span>
\t \t <span OnClick="AddDigit('4')">4</span>
\t \t <span OnClick="AddDigit('5')">5</span>
\t \t <span OnClick="AddDigit('6')">6</span>
\t \t <span OnClick="AddDigit('7')">7</span>
\t \t <span OnClick="AddDigit('8')">8</span>
\t \t <span OnClick="AddDigit('9')">9</span>
\t \t <span OnClick="AddDigit('0')" style="width: 166px;">0</span>
\t \t <span class="clear" OnClick="Clear()">
<div class="xBox">X</div>
</span>
\t </div>
</div>
</form>
我正在日期MM/DD/YYYY格式。以上代碼工作正常。它通過在數字之間自動添加/
來獲取數字。但是當用戶想要輸入日期如05/11/2016時,月份不允許在開始時輸入0。當用戶從小鍵盤點擊0,然後例如5時,它將0轉換爲5,它在開始時不佔用0。它增加了下一個點擊數字到月份。例如51/11/2016這樣。
我應該如何在月初允許0?
注:我有我的網頁設計,上面是像下圖:
用戶不應在文本框中直接鍵入。文本框應該有我設計的鍵盤輸入。因此,沒有在文本框如type="date"
上使用日期功能或使用datepicker或任何插件作爲用戶的使用不直接使用文本框。
你會使用http://momentjs.com/ –
節省大量的頭痛和測試你爲什麼要添加這個,'TYPE = 「電話」'嘗試'type =「date」' –
你爲什麼使用eval()? –