Thread: DOB validation

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

    DOB validation


    Hi, I am new member here and this is my first post.
    I am having prob.
    i want to validate user dob (only year) using java script.
    1-else if age is less then 20 message should be "not enough"
    2-else message should be "welcome".
    But it show "welcome" message if field is empty. please check and help me.any help will be appreciated.


    <html>
    <head>
    <title>
    This is dob validation
    </title>
    <script language="javascript">
    function dobvali()
    {
    var currentdate=new Date();
    var yearonly=currentdate.getFullYear();
    var usrdob=document.getElementById('dob').value;
    var res=yearonly-usrdob;
    if(res<20)
    {
    alert('not enough');
    }
    else
    {
    alert('welcome');
    }
    }
    </script>
    </head>
    <body>
    Enter DOB (DD/MM/YYYY):<input type="text" id="dob"/>
    <input type="button" value="check" onClick="dobvali();"/>
    </body>
    </html>
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,119
    Rep Power
    119
    The code below will do what your asking for, but you should also check / validate the month and day too; to ensure 100% accurate "Client Side" Age Verification. Plus you should also validate the format of your DOB text field.

    Code:
    <html>
    <head>
    <title>
    This is dob validation
    </title>
    <script type="text/javascript">
    function dobvali()
    {
    var currentdate = new Date();
    var yearonly = currentdate.getFullYear();
    var usrdob = parseInt(document.getElementById('dob').value.split("/")[2]);
    var res = yearonly - usrdob;
    if (usrdob.toString() != "undefined" && usrdob.toString().length == 4) {
    if(res<20)
    {
    alert('not enough');
    }
    else
    {
    alert('welcome');
    }
    }
    else if (usrdob.toString() == "NaN") {
    alert("Please Enter Numbers Only");
    }
    else {
    alert("Please Enter Your Date of Birth");
    }
    }
    </script>
    </head>
    <body>
    Enter DOB (DD/MM/YYYY):<input type="text" id="dob" maxlength="10"/>
    <input type="button" value="check" onClick="dobvali()"/>
    </body>
    </html>
    Last edited by web_loone08; November 12th, 2012 at 02:14 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0
    Ok brother i got it to some extent,please tell me what is the logic of using userdob="undefined" and userdob="NaN".
    I mean to say when a text_field is said to be "undefined" and when said to be "NaN".
    Also tell me how to find difference between two complete dates
    that mean DD/MM/YYYY 00:00:00.

    <html>
    <head>
    <title>
    This is dob validation
    </title>
    <script language="javascript">
    function dobvali()
    {
    var currentdate = new Date();
    var yearonly = currentdate.getFullYear();
    var usrdob = parseInt(document.getElementById('dob').value.split("/")[2]);
    var res = yearonly - usrdob;
    if (usrdob.toString() != "undefined" && usrdob.toString().length == 4) //userdob.toString() !='undefined' , please explain this.
    {
    if(res<20)
    {
    alert('not enough');
    }
    else
    {
    alert('welcome');
    }
    }
    else if (usrdob.toString() == "NaN")//userdob.toString() ='NaN' , please explain this.
    {
    alert("Please Enter Numbers Only");
    }
    else {
    alert("Please Enter Your Date of Birth");
    }
    }
    </script>
    </head>
    <body>
    Enter DOB (DD/MM/YYYY):<input type="text" id="dob" maxlength="10"/>
    <input type="button" value="check" onClick="dobvali()"/>
    </body>
    </html>
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,119
    Rep Power
    119
    Originally Posted by atta_akbar
    Ok brother i got it to some extent,please tell me what is the logic of using userdob="undefined" and userdob="NaN".
    I mean to say when a text_field is said to be "undefined" and when said to be "NaN".
    When I declared the variable "usrdob"; I parsed it as an interger, so I could do math (as it was originally a string). When I validated the "usrdob" variable; I converted back from an integer to a string. If the "usrdob" variable does not exsist, then the "usrdob" is "undefined" and if the "usrdob" variable is anything other then a integer (like any other character on your keyboard); you get "NaN", because it was originally parsed as an integer.

    Originally Posted by atta_akbar
    Also tell me how to find difference between two complete dates that mean DD/MM/YYYY 00:00:00.
    Do it similar to the way I did the year. Split it up into two sections and validate each section; with a specific validation criteria. Just look @ how I did the year; try to do the month and day the same way. I wouldn't worry about the time; that is to specific of a DOB validation - lol.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0
    thank you so much brother for this help , God Bless you. now i have understood.

IMN logo majestic logo threadwatch logo seochat tools logo