日期格式验证与jQuery [英] date format validation with jquery

查看:99
本文介绍了日期格式验证与jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试验证日期格式(来自datepicker),如果日期格式正确,则如果错误则转到下一步,然后发出警报

I am trying to validate the date format (from datepicker) If the date format is right then go to next step if wrong then alert

if ($("#date").val() != (/^\d{2}\/\d{2}\/\d{4}$/)) {
  $("#date").css('background-color', '#FF0000');
}
else {
  alert("good");
}

但是此脚本不起作用

推荐答案

您应该尝试:

if (!/^\d{2}\/\d{2}\/\d{4}$/.test($("#date").val())) {
  $("#date").css('background-color', '#FF0000');
}
else {
  alert("good");
}

您不能使用==!=运算符将字符串与RegExp对象进行比较,必须使用Regexp方法. 请参阅mdn

You can't compare a string to a RegExp object with == or != operators, you have to use Regexp methods. see mdn

这篇关于日期格式验证与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆