JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 12th, 2012, 01:32 PM
atta_akbar atta_akbar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 9 atta_akbar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 28 sec
Reputation 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>

Reply With Quote
  #2  
Old November 12th, 2012, 01:53 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 599 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 12 h 40 m
Reputation Power: 69
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.

Reply With Quote
  #3  
Old November 13th, 2012, 03:07 AM
atta_akbar atta_akbar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 9 atta_akbar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 28 sec
Reputation 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>

Reply With Quote
  #4  
Old November 13th, 2012, 08:01 PM
web_loone08's Avatar
web_loone08 web_loone08 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2008
Posts: 599 web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level)web_loone08 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 1 Day 12 h 40 m
Reputation Power: 69
Quote:
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.

Quote:
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.

Reply With Quote
  #5  
Old November 14th, 2012, 03:30 AM
atta_akbar atta_akbar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 9 atta_akbar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 41 m 28 sec
Reputation Power: 0
thank you so much brother for this help , God Bless you. now i have understood.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > DOB validation

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap