Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with regular expressions

    Hi everyone,

    I am trying to validate a date by using regular expressions.
    I have parts working, such as only accepts numbers, but I cannot get the range correct. On the "mm" field, I am getting errors.

    Code:
    if(form.mm.value=="")
    
    {
    	alert("Please insert your birth month");
    return false;}
    var re=/^(0[1-9]|1[0-2]){2}$/;
    if(!re.test(form.mm.value))
    {
    	alert("Please fix birth month!");
    return false;
    }
    {
    	if(form.dd.value=="")
    
    {
    	alert("Please insert your birth day");
    return false;}
    var re=/^\d{2}$/;
    if(!re.test(form.dd.value))
    {
    	alert("Please fix birth day!");
    return false;
    }
    {
    	if(form.yyyy.value=="")
    
    {
    	alert("Please insert your birth year");
    return false;}
    var re=/^\d{4}$/;
    if(!re.test(form.yyyy.value))
    {
    	alert("Please fix birth year!");
    return false;
    Any suggestions will be greatly appreciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    var re=/^(0[1-9])$|^(1[0-2])$/;

    var re=/^\d{2}$/; // but what about birthdays 1st - 9th?

    var re=/^\d{4}$/; // 4782???

    I would not try to use regular expressions here - just check day is in numeric range 1-31, month is in numeric range 1-12, year is sensible (say 1911 - 2011). But what if the user enters say 31/02/2009?

    You need to check that the date entered is in fact a valid date.

    Code:
    <script type = "text/javascript">
    
    function checkValidDate(yr,mmx,dd) {
    
    if (yr <1910 || yr >2010) {  // you may want to change 2010 to some other year!
    alert ("Impossible Year Of Birth!")
    return false;
    }
    
    mm = mmx-1;  // remember that in Javascript date objects the months are 0-11
    var nd = new Date();
    nd.setFullYear(yr,mm,dd);  // format YYYY,MM(0-11),DD
    
    var ndmm = nd.getMonth();
    if (ndmm != mm) {
    alert (dd + "/" + mmx + "/" + yr  + " is an Invalid Date!");
    return false; 
    }
    else {
    alert (dd + "/" + mmx + "/" + yr  + " is a Valid Date");
    }
    }
    
    checkValidDate(2010,2,31)  // 31st February 2009
    </script>



    His [Andy Murryay's] balls are 10 to 15% heavier now than at the start - Tennis commentator Radio5 Live
    Last edited by Philip M; 10-04-2011 at 09:22 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •