我想知道我是否可以获得一些帮助来尝试让此页面接受输入,然后指出它是否是有效的输入。
到目前为止我已经写了这个。
<html>
<head>
<title>JavaScript Date Strings</title>
<link rel="stylesheet" href="jsdatestrings.css" />
<script src="http://code.jquery.com/jquery.js"></script>
</head>
<body>
<h1 id="title">JavaScript Popup and Validation</h1>
<img class="popup-btn" src="death.jpg" />
<p>Das Datum (mm/dd/yyyy): <input id = "date" type="text" name="date"></p>
<script>
$(document).ready(function(){
$("input").blur(function(){
var x = $("#date").val();
$("#test").html(x);
});
});
</script>
<p id="test">HERRO!</p>
</body>
</html>
我不太确定如何获取它并使用它。我知道输入正在工作,但我需要得到类似 09281990 的东西,它应该给我类似 09/28/1990 的东西,如果我将其设置为 yyyy-mm-dd 那么它应该将其读取为无效,除非我输入 19900928。
欢迎任何帮助。谢谢。
最佳答案
$(document).ready(function(){
$("input").blur(function(){
var x = $("#date").val();
var day = x.substring(0,2);
var month = x.substring(2,4);
var year = x.substring(4,8);
re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
if(!(day+'/'+month+'/'+year).match(re)) {
alert("Invalid date format: " + day+'/'+month+'/'+year);
}else{
$("#test").html(day+'/'+month+'/'+year);
} });
});
Try this..
关于javascript - 如何解析此 HTML javascript 页面中的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18842854/