这是我的代码:
Ensure-ExecutableExists -Executable pscp -MinimumVersion "0.62.0.0"
$('#test').on('click',function(){
var regdate = new RegExp('^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2})$|^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2}\s([0-1]\d|[2][0-3])\:[0-5]\d\:[0-5]\d)$');
if (regdate.test($('#datepicker').val()))
{
$('#response').text("Okay");
}
else
{
$('#response').text("Nooo");
}
})
当我们尝试<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="form-control noColorFocus" name="dateMinChargement" id="datepicker" />
<button id="test">Valider</button>
<div id="response"></div>
时,17/01/2017
之类的日期应该返回true
...有什么想法吗?
答案 0 :(得分:2)
宣布正则表达式时,您有两种选择:
new RegExp('...')
需要转义反斜杠/MyPattern/
反斜杠不再是问题所在:
$('#test').on('click',function(){
var regdate = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2})$|^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[2][0]\d{2}\s([0-1]\d|[2][0-3])\:[0-5]\d\:[0-5]\d)$/;
if (regdate.test($('#datepicker').val()))
$('#response').text("Okay");
else
$('#response').text("Nooo");
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="form-control noColorFocus" name="dateMinChargement" id="datepicker" />
<button id="test">Valider</button>
<div id="response"></div>
&#13;