0

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;
   }
  patt2=/([ ./-])\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;
   }    
 }
Eimantas
  • 48,927
  • 17
  • 132
  • 168
willis0924
  • 67
  • 5
  • 8

5 Answers5

0

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
}
}
TrL
  • 32
  • 2
0

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.

Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
  • @alex I'm just giving him a hint about another way he could do the same, although I agree with your comment under his question: If he needs to tell the user what was written bad, he'll have to do something like that, because if the date is incorrect, the regex in my answer won't just match, with the exception of the year, where he will need to check if the last matched group is greather than `2011`. – Oscar Mederos Apr 25 '11 at 06:02
0

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.

GAgnew
  • 3,847
  • 3
  • 26
  • 28
  • 1
    More painful than adding global variables needlessly and missing semicolons? :P – alex Apr 25 '11 at 05:52
  • context not included :/ and yes, I know how the the automatic semicolon insertion works, so I choose to omit them in places for legibility. In fact, I'm going to remove the others. – GAgnew Apr 25 '11 at 05:54
0
//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
JWL
  • 13,591
  • 7
  • 57
  • 63
0

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)
kennebec
  • 102,654
  • 32
  • 106
  • 127