好吧,我在表單中輸入了一個日期輸入。我希望它接受基於像「XX/XX/XXXX」這樣的模式的字符。目前它只接受數字和「/」。但我不希望用戶輸入像「X/XXXX/XX」或其他類型的日期,我不想像「// XX/XXX/XXX」那樣寫錯。我不知道如何在這一個上使用js,或者jQuery,所以用戶根據模式輸入日期,而不是以其他方式輸入。JavaScript/jQuery input [type = text] pattern
1
A
回答
1
1
也許你可以看看meiomask jQuery插件這是一個最好的,以用於應用模式的輸入,並添加automaticaly了「/」的日期。
3
廉價和簡單的方法可能只是實施jQuery UI datepicker控制,以便您可以通過它來控制輸入格式。
它基本上是一步步解決問題,但可能會將實際問題排除在等式之外。
0
使用正則表達式^(3[0-1]|[0-2]?[0-9])\/(0?[0-9]|1[0-2])\/(2[0-9]+)$
:
var date = "20/12/2012";
console.log(isDateOK(date));
function isDateOK(date) {
return date.match(/^(3[0-1]|[0-2]?[0-9])\/(0?[0-9]|1[0-2])\/([1-2][0-9]{3})$/);
}
它將返回false,如果它不匹配。本例使用DD/MM/YYYY。如果你想MM/DD/YYYY,使用下面的正則表達式來代替:^(0?[0-9]|1[0-2])\/(3[0-1]|[0-2]?[0-9])\/[1-2][0-9]{3$
的jsfiddle:http://jsfiddle.net/UBFeN/2/
相關問題
- 1. HTML <input type =「text」... as <input type =「file」
- 2. Jquery $(:text)and $(input [type =「text」])
- 3. input type =「text」not resizing
- 4. input [type = text] Vs.類細節
- 5. 增加<input type =「text」>
- 6. <input type =「text」/>換行
- 7. <input type =「submit」/>和<input type =「text」/>
- 8. input type =「date」text-align:right IOS設備
- 9. input [type =「text」]:禁用不適用於IE
- 10. 另一個ASP:Textbox vs <input type =「text」>
- 11. 拆分<input type =「text」>使用Angular.js
- 12. 從input type ='text'創建元素JavaScript
- 13. 從{{view Ember.TextField}}過渡到{{input type =「text」}}
- 14. 結合INPUT TYPE =「text」和選擇標籤
- 15. swift osx webview input type = text not working
- 16. 我可以使用<input type =「text」>來模擬<input type =「file」>嗎?
- 17. 如何在<input type =「text」>標籤內放置<input type =「file」>?
- 18. 如何使用jQuery替換input [type = submit]輸入[type = text]?
- 19. 使用AngularJS和Pattern/RegEx驗證<input type =「number」/>
- 20. Scala Number type pattern matching
- 21. INPUT TYPE =在Safari
- 22. input [type = file] validation
- 23. <input type =「file」/>
- 24. 如何將<input type =「image」>綁定到<input type =「text」>?兩者都是相同的形式
- 25. <input type ='text'/>和<textarea>寬度
- 26. 在<input type =「text」/>中輸入文字描邊
- 27. 如何用var javascript onchange甚至更新input type =「text」值?
- 28. php form input type =「text」具體字段下面的回顯標籤
- 29. PHP將文本發送到<input type =「text」>
- 30. <asp:TextBox> Vs <input type =「text」> Vs Html.TextBox
對於屏蔽輸入,請記住,你要能夠接受部分有效輸入,以及。你不應該阻止'XX/X/XXXX',因爲那樣你會阻止用戶糾正一個錯字 - 這是非常令人沮喪的。你想要做的是通過屏蔽拒絕明顯無效的輸入(例如你的案例中的字母字符),然後檢查當輸入字段失去焦點或表單被提交時最終輸入是否無效。 (同樣令人沮喪的是:阻止用戶退出無效字段。) – millimoose
尚未得到廣泛支持,但HTML5的''具有適當的UI。 – pimvdb