法语日期的正则表达式不起作用

时间:2017-02-01 15:52:23

标签: jquery html regex

这是我的代码:

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 ...有什么想法吗?

1 个答案:

答案 0 :(得分:2)

宣布正则表达式时,您有两种选择:

  1. 字符串:new RegExp('...')需要转义反斜杠
  2. 直接:/MyPattern/反斜杠不再是问题所在:
  3. &#13;
    &#13;
    $('#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;
    &#13;
    &#13;