admin管理员组文章数量:1406943
Value will be anything and matches is null. The point of this is to split up a string like "1991-12-01" and make sure that all of the parts of the string are valid dates.
dateISO: function(value, element) {
if (this.optional(element)) return true;
var regexp = new RegExp('^\d{4}[\/-](\d{1,2})[\/-](\d{1,2})$');
var matches = regexp.exec(value);
alert(matches);
Any ideas?
Value will be anything and matches is null. The point of this is to split up a string like "1991-12-01" and make sure that all of the parts of the string are valid dates.
dateISO: function(value, element) {
if (this.optional(element)) return true;
var regexp = new RegExp('^\d{4}[\/-](\d{1,2})[\/-](\d{1,2})$');
var matches = regexp.exec(value);
alert(matches);
Any ideas?
Share asked Aug 19, 2009 at 17:32 Ian McCulloughIan McCullough 1,4434 gold badges25 silver badges36 bronze badges2 Answers
Reset to default 7The expression you're giving is a string, thus, needs escaping:
var regexp = new RegExp('^\\d{4}[\\/-](\\d{1,2})[\\/-](\\d{1,2})$');
Alternatively, you can do the perl-styled expressions, but slashes need to be escaped:
var regexp = /^\d{4}[\\/-](\d{1,2})[\\/-](\d{1,2})$/;
(The perl-styled regular expression returns a RegExp object)
Why not just skip regex altogether here? I know this is way more code, but it will give errors for dates that a regex can't, like a date specifying November 31st.
var datesToTest = [
"1991-12-01" // valid
, "1991-11-31" // invalid, Nov has 30 days
, "1991-13-01" // invalid, no 13th month
];
// Note: console.log() requires Firebug - switch to alert() if you wish
for ( var i = 0; i < datesToTest.length; i++ )
{
if ( !isValidDate( datesToTest[i], '-' ) )
{
console.log( datesToTest[i] + ' is not a valid date!' );
} else {
console.log( datesToTest[i] + ' is valid date!' );
}
}
function isValidDate( dateAsString, delimiter )
{
var dateObject = new Date( dateAsString.replace( new RegExp( delimiter, 'g' ), '/' ) );
var checkDate = [
dateObject.getFullYear()
, zeroFill( dateObject.getMonth() + 1, 2 )
, zeroFill( dateObject.getDate(), 2 )
].join( delimiter );
return ( dateAsString == checkDate );
}
function zeroFill( number, width )
{
width -= number.toString().length;
if ( width > 0 )
{
return new Array( width + (/\./.test( number ) ? 2 : 1) ).join( '0' ) + number;
}
return number;
}
This will work as long as you don't need to validate a date before 100 AD =P
本文标签: javascriptWhats wrong with this jQuery validation code regexpexec(value)Stack Overflow
版权声明:本文标题:javascript - Whats wrong with this jQuery validation code? regexp.exec(value) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744926184a2632611.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论