SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating Birthdate Field

    Hello,

    What is the best way to validate a form field that users enter their date of birth as in dd/mm/yyyy. Currently the form has 3 different drop down menus for the day, month and year. Because the database only contains 1 field for the Birthdate and cannot be modified, I must change it to a text field. How can I make sure that people enter the correct format (dd/mm/yyyy) and not Jan. 14th 2007 or something...

    Thanks!


  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var regex = /\d{2}\/\d{2}\/\d{4}/;
    
    if(regex.test(someInputString))
        //then valid
    A regex begins and ends with a '/'. The slashes serve to delineate a regex, just like quote marks delineate a string, e.g.:

    var str = "hello";

    So, if you want to actually match a '/' in your regex, you can't write it as a '/'--otherwise js will think it is the end of the regex. That happens with strings too; you can't write:

    var str = "Hello" there";

    So, to match an actual '/', you have to escape it. You escape a character with a backslash '\', so to match a '/', you have to write the strange looking hieroglyphic: '\/'.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud View Post
    Code:
    var regex = /\d{2}\/\d{2}\/\d{4}/;
    
    if(regex.test(someInputString))
        //then valid
    .
    Thanks... so where do I add this code? Let's say that this is my Birthdate field:
    <input type="text" name="datebirth" size="10">
    Do I add code in the field line to call the regex function. Also, do I place the regex function below the field or at the top of the page?

    Thank you for clarifying!

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks... so where do I add this code?
    Buy a beginning javascript book, like Javascript: A Beginner's Guide, and and keep reading it until you can figure that out.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot 7stud. I'm not very familliar with JavaScript but this is why this forum exist. If the answer to every question posted on this forum was "go read a beginners book", Sitepoint wouldn't exist.

    Can anyone esle out there help me? I need to fix this quickly... then I'll have time to read a book :-)

    Julie

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the answer to every question posted on this forum was "go read a beginners book", Sitepoint wouldn't exist.
    True, and your point is well taken. However, this forum is not a place to procure free web programming services. Instead, this forum is a place where people who are learning javascript can ask questions, and it's also a place for people who are facile with javascript to discuss technical questions or ideas they have.

    Also, do I place the regex function below the field or at the top of the page?
    The best practice is to put all javascript in a separate file that you name with a .js extension. You can then link to that file using <script >tags containing a src atrribute that you put in the <head>.

    The next best thing is to put all the js code in the <head> between <script> tags (with no src attribute).

    However, beginners generally start out by putting event handler functions inline in the html, e.g.

    <div onclick="someFunc()">click me</div>

    where someFunc() is defined between a set of <script> tags in the <head>.
    Last edited by 7stud; Feb 8, 2007 at 14:33.

  7. #7
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    For a more accurate date validation you could use:

    /^(0?[1-9]|[12][0-9]|3[01])[\/\.- ](0?[1-9]|1[0-2])[\/\.- ](19|20)\d{2}$/

    which validates that the day portion is between 1 and 31, the month between 1 and 12 and year between 1900 and 2099
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  8. #8
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    jWeb- If I have my druthers, I use the three select menu system you are trying to replace with a text input. They are familiar to most people, and they are self validating- except for telling you if someone is lying about their age.

    You can build a single date string from the three selected indexes.
    Set the string as the value of a readonly or hidden input that is sent to the server.
    Give the selects id's instead of names, so they won't be sent with the other values.

    The best date pickers adjust the date count of the days menu on the change events of the month and year fields,
    so you won't get April 31, or February 29 if the year of birth is not a leap year.


    I'll give you a head start- this script will return the number of days in the month and year of the Date object that calls it-

    var D=new Date(1954,3);//remember, months start with 0, so 3=April
    alert(D.monthDays());

    Code:
    Date.prototype.monthDays= function(){
    var m= this.getMonth()+1;		
    switch(m){
    	case 4: case 6: case 9: case 11: return 30;
    	case 2:
    	var dy= this.getFullYear();
    	if(dy&#37;400 ==0 || (dy%100 !=0 && dy%4==0)) return 29;
    	return 28;
    
    	default: return 31;
    	}
    }
    Last edited by mrhoo; Feb 8, 2007 at 08:48. Reason: spell check

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for all the tips. I'll practice with all above suggestions.

    Julie


Bookmarks

Posting Permissions

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