Out of curiosity, is there anyone out there that would know of a way to simplify this process. I am writing a regular expression for a date validator that only accepts the mm.dd.yyyy mm/dd/yyyy, or mm-dd-yyyy formats. This way works, I think, but it seems really drawn out.
function c(x, y)
{
//check that there is a date and in right order
if ((x==1) || (x==3))
{
if( y == "")
{alert("You have not entered a date");
return false;
}
var string = document.getElementById('date');
var w = string.value.search(/^(\d{2})([ ./-]{1})(\d{2})\2(\d{4})$/);
if (w != 0)
{
alert("Bad Date pattern match please redo");
return false;
}
var patt=/\d{2}/
var result=patt.exec(string.value);
if(result > 12)
{
alert("Please redo Date(Month)");
return false;
}
patt开发者_如何学运维2=/([ ./-])\d{2}\1/
result=patt2.exec(string.value);
result=patt.exec(result);
if(result > 31)
{
alert("Please redo Date(Days)");
return false;
}
patt=/([ ./-])\d{4}/
result=patt.exec(string.value);
patt2=/\d{4}/
result=patt2.exec(result);
if(result > 2011)
{
alert("Please redo Date(Years)");
return false;
}
}
//The OP asked for a simpler way, and here it is:
function validateDate(dateValue)
{
//improving the regular expression to do even more wonders
//is left as an exercise :-)
var pattern = /^(0[1-9]|1[012])[/.-](0[1-9]|[12][0-9]|3[01])[/.-](19|20)\d\d$/
if(pattern.test(dateValue) == true)
alert('valid date');
else
alert('invalid date');
}
//OK, I don't have lots of the fine-grained error messages in,
//but all 3 formats should pass this
Did you take a look at Regular Expression Matching a Valid Date?
Here's an example of a regex for parsing mm/dd/yyyy
.
^(0[1-9]|1[012])/(0[1-9]|[12][0-9]|3[01])/(19|20)\d\d$
You can change delimiters to parse mm.dd.yyyy
and mm-dd-yyyy
.
That code you posted is painful to my eyes. Please stop.
x = 'mm-dd-yyyy'
xArray = x.split('-')
for(var i = 0; i < 3; i++)
(isNumeric(xArray[0])) ? null : return false
The code is the same for other separators, just check for which separator exists. You may have to elaborate on this to get all the desired assertions, but it shows you how.
Regular expressions are BAD and SLOW avoid them as much as possible.
A regular expression has to be a monster to catch any incorrect input, like feb 29, 2011.
Simpler is to try to make a date of the input, then check to be sure 2/29 did not evaluate to 3/1.
function isvalid_mdy(s){
var day, A= s.split(/\D+/);
A[0]= parseInt(A[0], 10)-1;
A[1]= parseInt(A[1], 10);
A[2]= parseInt(A[2], 10);
try{
day= new Date(A[2], A[0], A[1]);
if(day.getMonth()== A[0] && day.getDate()== A[1]) return day;
throw new Error('Bad Date '+s);
}
catch(er){
alert(er.message)
return NaN;
}
}
var s1= '04/31/2011';
isvalid_mdy(s1)
This?
function c(a, b) {
if (a == 1 || a == 3) {
if (b == "") return alert("You have not entered a date"), !1;
var c = document.getElementById("date"),
d = c.value.search(/^(\d{2})([ ./-]{1})(\d{2})\2(\d{4})$/);
if (d != 0) return alert("Bad Date pattern match please redo"), !1;
var e = /\d{2}/,
f = e.exec(c.value);
if (f > 12) return alert("Please redo Date(Month)"), !1;
if (patt2 = /([ ./-])\d{2}\1/, f = patt2.exec(c.value), f = e.exec(f), f > 31) return alert("Please redo Date(Days)"), !1;
if (e = /([ ./-])\d{4}/, f = e.exec(c.value), patt2 = /\d{4}/, f = patt2.exec(f), f > 2011) return alert("Please redo Date(Years)"), !1
}
}
精彩评论