How to validate DOB from dropdown in javascript?Validate decimal numbers in JavaScript - IsNumeric()How to validate an email address in JavaScriptHow do JavaScript closures work?How to validate an email address using a regular expression?How do I remove a property from a JavaScript object?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
Did the US Climate Reference Network Show No New Warming Since 2005 in the US?
How do I make my fill-in-the-blank exercise more obvious?
If magnetic force can't do any work, then how can we define a potential?
What's the point of this macro?
Why is a pressure canner needed when canning?
Is it possible to observe space debris with Binoculars?
Draw the ☣ (Biohazard Symbol)
Is mathematics truth?
Tiny image scraper for xkcd.com
Professor refuses to write a recommendation letter
Round away from zero
Is using different public keys for different peers safer than reusing the public key, beyond forward secrecy - x25519
Why is に used with this verb?
Are treasury bonds more liquid than USD?
Why do we need explainable AI?
What are some countries where you can be imprisoned for reading or owning a Bible?
What is the statistical difference between "choose either total" or "choose new total" when rerolling damage die?
Are language and thought the same?
Is directly echoing the user agent in PHP a security hole?
Time to call the bluff
Why do old games use flashing as means of showing damage?
How many people can lift Thor's hammer?
Were the women of Travancore, India, taxed for covering their breasts by breast size?
How could a planet have one hemisphere way warmer than the other without the planet being tidally locked?
How to validate DOB from dropdown in javascript?
Validate decimal numbers in JavaScript - IsNumeric()How to validate an email address in JavaScriptHow do JavaScript closures work?How to validate an email address using a regular expression?How do I remove a property from a JavaScript object?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm creating a html form and the form should be validated using Javascript.
While registering, the age of user should be greater than 16 years and I'm taking DOB from dropdown.
Now, my question is how to validate DOB using javscript and user's age shouldn't be greater than 16
I've validated the DOB as you can see in the code but that is not proper way of validation
<head>
<script type="text/javascript">
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var year1 = document.getElementById("year").value;
var month1 = document.getElementById("month").value;
var day1 = document.getElementById("day").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(day1==0)
alert("Please select Day")
else if(month1==0)
alert("Please select Month")
else if(year1==0)
alert("Please select year")
else if(year1>2003)
alert("You are not eligible. Age should be above 16...!!!")
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
</script>
</head>
<body>
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <br>
<select id="day">
<option value="0">day</option>
<option value="1">1</option>
<option value="2">2</option>
......
<option value="31">31</option>
</select>
<select id="month">
<option value="0">Month</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>......
<option value="12">12</option>
</select>
<select id="year">
<option value="0">Year</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>......
<option value="2019">2019</option>
</select>
Male
Female
SignUp
javascript html forms validation
add a comment |
I'm creating a html form and the form should be validated using Javascript.
While registering, the age of user should be greater than 16 years and I'm taking DOB from dropdown.
Now, my question is how to validate DOB using javscript and user's age shouldn't be greater than 16
I've validated the DOB as you can see in the code but that is not proper way of validation
<head>
<script type="text/javascript">
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var year1 = document.getElementById("year").value;
var month1 = document.getElementById("month").value;
var day1 = document.getElementById("day").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(day1==0)
alert("Please select Day")
else if(month1==0)
alert("Please select Month")
else if(year1==0)
alert("Please select year")
else if(year1>2003)
alert("You are not eligible. Age should be above 16...!!!")
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
</script>
</head>
<body>
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <br>
<select id="day">
<option value="0">day</option>
<option value="1">1</option>
<option value="2">2</option>
......
<option value="31">31</option>
</select>
<select id="month">
<option value="0">Month</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>......
<option value="12">12</option>
</select>
<select id="year">
<option value="0">Year</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>......
<option value="2019">2019</option>
</select>
Male
Female
SignUp
javascript html forms validation
add a comment |
I'm creating a html form and the form should be validated using Javascript.
While registering, the age of user should be greater than 16 years and I'm taking DOB from dropdown.
Now, my question is how to validate DOB using javscript and user's age shouldn't be greater than 16
I've validated the DOB as you can see in the code but that is not proper way of validation
<head>
<script type="text/javascript">
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var year1 = document.getElementById("year").value;
var month1 = document.getElementById("month").value;
var day1 = document.getElementById("day").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(day1==0)
alert("Please select Day")
else if(month1==0)
alert("Please select Month")
else if(year1==0)
alert("Please select year")
else if(year1>2003)
alert("You are not eligible. Age should be above 16...!!!")
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
</script>
</head>
<body>
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <br>
<select id="day">
<option value="0">day</option>
<option value="1">1</option>
<option value="2">2</option>
......
<option value="31">31</option>
</select>
<select id="month">
<option value="0">Month</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>......
<option value="12">12</option>
</select>
<select id="year">
<option value="0">Year</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>......
<option value="2019">2019</option>
</select>
Male
Female
SignUp
javascript html forms validation
I'm creating a html form and the form should be validated using Javascript.
While registering, the age of user should be greater than 16 years and I'm taking DOB from dropdown.
Now, my question is how to validate DOB using javscript and user's age shouldn't be greater than 16
I've validated the DOB as you can see in the code but that is not proper way of validation
<head>
<script type="text/javascript">
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var year1 = document.getElementById("year").value;
var month1 = document.getElementById("month").value;
var day1 = document.getElementById("day").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(day1==0)
alert("Please select Day")
else if(month1==0)
alert("Please select Month")
else if(year1==0)
alert("Please select year")
else if(year1>2003)
alert("You are not eligible. Age should be above 16...!!!")
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
</script>
</head>
<body>
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <br>
<select id="day">
<option value="0">day</option>
<option value="1">1</option>
<option value="2">2</option>
......
<option value="31">31</option>
</select>
<select id="month">
<option value="0">Month</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>......
<option value="12">12</option>
</select>
<select id="year">
<option value="0">Year</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>......
<option value="2019">2019</option>
</select>
Male
Female
SignUp
javascript html forms validation
javascript html forms validation
edited Apr 3 at 6:20
Cody Gray♦
201k38 gold badges403 silver badges487 bronze badges
201k38 gold badges403 silver badges487 bronze badges
asked Mar 28 at 3:50
Vishal UpadhyayVishal Upadhyay
13 bronze badges
13 bronze badges
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Please try this,
NOTE : Try to use a date picker for date selection
function validateDOB() (m === 0 && today.getDate() < birthDate.getDate()))
age--;
if(age<16)
alert('You are not eligible. Age should be above 16...!!!')
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>add a comment |
Since your codes have checked whether the user has selected year, month and date of birth, so you just call my function after your checking is ok.
function isValidDate(year, month, date)
var dob = new Date(year, month-1, date);
if (dob.getDate() != date)
alert("Invalid date value");
else if ((dob.getMonth() - month) != -1)
alert("Invalid month value");
else if (dob.getFullYear() != year)
alert("Invalid year value");
add a comment |
Try this,
function validateDOB()
if($('#dob').val()=='')
alert('select dob')
return false
var dob=$('#dob').val().replace('-', '')
var age= moment().diff(moment(dob, 'YYYYMMDD'), 'years');
if(age<16)
console.log(age)
alert('You are not eligible. Age should be above 16...!!!')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>
The above prob is a js validation .anyways that was a good effort
– Deepak A
Mar 28 at 4:33
This is a slick solution that works well, but could you please describe how this works? I see there are a couple libraries included that the OP hadn't used in their example
– tshimkus
Mar 28 at 4:33
it uses moment.js and jquery.min.js lib
– Deepak A
Mar 28 at 4:33
1
Yes, I noticed that, just trying to encourage @mariya sunny to write a well formed answer that can help the community. We are building a knowledge base together
– tshimkus
Mar 28 at 4:35
1
@tshimkus , Deepak. Thank you for your support i will try to improve my answer
– user11269831
Mar 28 at 4:43
add a comment |
Javascript code
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var birthday = document.getElementById("bday").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(!birthday) //To validate if birth date is empty
alert("Please select Birth Date")
else if(submitBday('bday')>16)//To check age greater than 16
alert("Age should be greater than 16");
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
function submitBday(id)
var Bdate = document.getElementById(id).value;
var Bday = +new Date(Bdate);
var Q4A = ~~ ((Date.now() - Bday) / (31557600000));
return Q4A;
your html code
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <input type="date" name="bday" id="bday"">
reference http://jsfiddle.net/j08691/exVh2/1/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389890%2fhow-to-validate-dob-from-dropdown-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please try this,
NOTE : Try to use a date picker for date selection
function validateDOB() (m === 0 && today.getDate() < birthDate.getDate()))
age--;
if(age<16)
alert('You are not eligible. Age should be above 16...!!!')
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>add a comment |
Please try this,
NOTE : Try to use a date picker for date selection
function validateDOB() (m === 0 && today.getDate() < birthDate.getDate()))
age--;
if(age<16)
alert('You are not eligible. Age should be above 16...!!!')
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>add a comment |
Please try this,
NOTE : Try to use a date picker for date selection
function validateDOB() (m === 0 && today.getDate() < birthDate.getDate()))
age--;
if(age<16)
alert('You are not eligible. Age should be above 16...!!!')
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>Please try this,
NOTE : Try to use a date picker for date selection
function validateDOB() (m === 0 && today.getDate() < birthDate.getDate()))
age--;
if(age<16)
alert('You are not eligible. Age should be above 16...!!!')
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>function validateDOB() (m === 0 && today.getDate() < birthDate.getDate()))
age--;
if(age<16)
alert('You are not eligible. Age should be above 16...!!!')
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>function validateDOB() (m === 0 && today.getDate() < birthDate.getDate()))
age--;
if(age<16)
alert('You are not eligible. Age should be above 16...!!!')
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>answered Mar 28 at 4:07
JIJOMON K.AJIJOMON K.A
1,1412 gold badges7 silver badges24 bronze badges
1,1412 gold badges7 silver badges24 bronze badges
add a comment |
add a comment |
Since your codes have checked whether the user has selected year, month and date of birth, so you just call my function after your checking is ok.
function isValidDate(year, month, date)
var dob = new Date(year, month-1, date);
if (dob.getDate() != date)
alert("Invalid date value");
else if ((dob.getMonth() - month) != -1)
alert("Invalid month value");
else if (dob.getFullYear() != year)
alert("Invalid year value");
add a comment |
Since your codes have checked whether the user has selected year, month and date of birth, so you just call my function after your checking is ok.
function isValidDate(year, month, date)
var dob = new Date(year, month-1, date);
if (dob.getDate() != date)
alert("Invalid date value");
else if ((dob.getMonth() - month) != -1)
alert("Invalid month value");
else if (dob.getFullYear() != year)
alert("Invalid year value");
add a comment |
Since your codes have checked whether the user has selected year, month and date of birth, so you just call my function after your checking is ok.
function isValidDate(year, month, date)
var dob = new Date(year, month-1, date);
if (dob.getDate() != date)
alert("Invalid date value");
else if ((dob.getMonth() - month) != -1)
alert("Invalid month value");
else if (dob.getFullYear() != year)
alert("Invalid year value");
Since your codes have checked whether the user has selected year, month and date of birth, so you just call my function after your checking is ok.
function isValidDate(year, month, date)
var dob = new Date(year, month-1, date);
if (dob.getDate() != date)
alert("Invalid date value");
else if ((dob.getMonth() - month) != -1)
alert("Invalid month value");
else if (dob.getFullYear() != year)
alert("Invalid year value");
edited Mar 28 at 4:18
answered Mar 28 at 4:05
The KNVBThe KNVB
1,0121 gold badge12 silver badges20 bronze badges
1,0121 gold badge12 silver badges20 bronze badges
add a comment |
add a comment |
Try this,
function validateDOB()
if($('#dob').val()=='')
alert('select dob')
return false
var dob=$('#dob').val().replace('-', '')
var age= moment().diff(moment(dob, 'YYYYMMDD'), 'years');
if(age<16)
console.log(age)
alert('You are not eligible. Age should be above 16...!!!')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>
The above prob is a js validation .anyways that was a good effort
– Deepak A
Mar 28 at 4:33
This is a slick solution that works well, but could you please describe how this works? I see there are a couple libraries included that the OP hadn't used in their example
– tshimkus
Mar 28 at 4:33
it uses moment.js and jquery.min.js lib
– Deepak A
Mar 28 at 4:33
1
Yes, I noticed that, just trying to encourage @mariya sunny to write a well formed answer that can help the community. We are building a knowledge base together
– tshimkus
Mar 28 at 4:35
1
@tshimkus , Deepak. Thank you for your support i will try to improve my answer
– user11269831
Mar 28 at 4:43
add a comment |
Try this,
function validateDOB()
if($('#dob').val()=='')
alert('select dob')
return false
var dob=$('#dob').val().replace('-', '')
var age= moment().diff(moment(dob, 'YYYYMMDD'), 'years');
if(age<16)
console.log(age)
alert('You are not eligible. Age should be above 16...!!!')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>
The above prob is a js validation .anyways that was a good effort
– Deepak A
Mar 28 at 4:33
This is a slick solution that works well, but could you please describe how this works? I see there are a couple libraries included that the OP hadn't used in their example
– tshimkus
Mar 28 at 4:33
it uses moment.js and jquery.min.js lib
– Deepak A
Mar 28 at 4:33
1
Yes, I noticed that, just trying to encourage @mariya sunny to write a well formed answer that can help the community. We are building a knowledge base together
– tshimkus
Mar 28 at 4:35
1
@tshimkus , Deepak. Thank you for your support i will try to improve my answer
– user11269831
Mar 28 at 4:43
add a comment |
Try this,
function validateDOB()
if($('#dob').val()=='')
alert('select dob')
return false
var dob=$('#dob').val().replace('-', '')
var age= moment().diff(moment(dob, 'YYYYMMDD'), 'years');
if(age<16)
console.log(age)
alert('You are not eligible. Age should be above 16...!!!')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>Try this,
function validateDOB()
if($('#dob').val()=='')
alert('select dob')
return false
var dob=$('#dob').val().replace('-', '')
var age= moment().diff(moment(dob, 'YYYYMMDD'), 'years');
if(age<16)
console.log(age)
alert('You are not eligible. Age should be above 16...!!!')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>function validateDOB()
if($('#dob').val()=='')
alert('select dob')
return false
var dob=$('#dob').val().replace('-', '')
var age= moment().diff(moment(dob, 'YYYYMMDD'), 'years');
if(age<16)
console.log(age)
alert('You are not eligible. Age should be above 16...!!!')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>function validateDOB()
if($('#dob').val()=='')
alert('select dob')
return false
var dob=$('#dob').val().replace('-', '')
var age= moment().diff(moment(dob, 'YYYYMMDD'), 'years');
if(age<16)
console.log(age)
alert('You are not eligible. Age should be above 16...!!!')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<input type='date' id='dob'>
<button onclick='validateDOB()'>validate</button>answered Mar 28 at 4:24
user11269831
The above prob is a js validation .anyways that was a good effort
– Deepak A
Mar 28 at 4:33
This is a slick solution that works well, but could you please describe how this works? I see there are a couple libraries included that the OP hadn't used in their example
– tshimkus
Mar 28 at 4:33
it uses moment.js and jquery.min.js lib
– Deepak A
Mar 28 at 4:33
1
Yes, I noticed that, just trying to encourage @mariya sunny to write a well formed answer that can help the community. We are building a knowledge base together
– tshimkus
Mar 28 at 4:35
1
@tshimkus , Deepak. Thank you for your support i will try to improve my answer
– user11269831
Mar 28 at 4:43
add a comment |
The above prob is a js validation .anyways that was a good effort
– Deepak A
Mar 28 at 4:33
This is a slick solution that works well, but could you please describe how this works? I see there are a couple libraries included that the OP hadn't used in their example
– tshimkus
Mar 28 at 4:33
it uses moment.js and jquery.min.js lib
– Deepak A
Mar 28 at 4:33
1
Yes, I noticed that, just trying to encourage @mariya sunny to write a well formed answer that can help the community. We are building a knowledge base together
– tshimkus
Mar 28 at 4:35
1
@tshimkus , Deepak. Thank you for your support i will try to improve my answer
– user11269831
Mar 28 at 4:43
The above prob is a js validation .anyways that was a good effort
– Deepak A
Mar 28 at 4:33
The above prob is a js validation .anyways that was a good effort
– Deepak A
Mar 28 at 4:33
This is a slick solution that works well, but could you please describe how this works? I see there are a couple libraries included that the OP hadn't used in their example
– tshimkus
Mar 28 at 4:33
This is a slick solution that works well, but could you please describe how this works? I see there are a couple libraries included that the OP hadn't used in their example
– tshimkus
Mar 28 at 4:33
it uses moment.js and jquery.min.js lib
– Deepak A
Mar 28 at 4:33
it uses moment.js and jquery.min.js lib
– Deepak A
Mar 28 at 4:33
1
1
Yes, I noticed that, just trying to encourage @mariya sunny to write a well formed answer that can help the community. We are building a knowledge base together
– tshimkus
Mar 28 at 4:35
Yes, I noticed that, just trying to encourage @mariya sunny to write a well formed answer that can help the community. We are building a knowledge base together
– tshimkus
Mar 28 at 4:35
1
1
@tshimkus , Deepak. Thank you for your support i will try to improve my answer
– user11269831
Mar 28 at 4:43
@tshimkus , Deepak. Thank you for your support i will try to improve my answer
– user11269831
Mar 28 at 4:43
add a comment |
Javascript code
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var birthday = document.getElementById("bday").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(!birthday) //To validate if birth date is empty
alert("Please select Birth Date")
else if(submitBday('bday')>16)//To check age greater than 16
alert("Age should be greater than 16");
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
function submitBday(id)
var Bdate = document.getElementById(id).value;
var Bday = +new Date(Bdate);
var Q4A = ~~ ((Date.now() - Bday) / (31557600000));
return Q4A;
your html code
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <input type="date" name="bday" id="bday"">
reference http://jsfiddle.net/j08691/exVh2/1/
add a comment |
Javascript code
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var birthday = document.getElementById("bday").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(!birthday) //To validate if birth date is empty
alert("Please select Birth Date")
else if(submitBday('bday')>16)//To check age greater than 16
alert("Age should be greater than 16");
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
function submitBday(id)
var Bdate = document.getElementById(id).value;
var Bday = +new Date(Bdate);
var Q4A = ~~ ((Date.now() - Bday) / (31557600000));
return Q4A;
your html code
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <input type="date" name="bday" id="bday"">
reference http://jsfiddle.net/j08691/exVh2/1/
add a comment |
Javascript code
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var birthday = document.getElementById("bday").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(!birthday) //To validate if birth date is empty
alert("Please select Birth Date")
else if(submitBday('bday')>16)//To check age greater than 16
alert("Age should be greater than 16");
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
function submitBday(id)
var Bdate = document.getElementById(id).value;
var Bday = +new Date(Bdate);
var Q4A = ~~ ((Date.now() - Bday) / (31557600000));
return Q4A;
your html code
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <input type="date" name="bday" id="bday"">
reference http://jsfiddle.net/j08691/exVh2/1/
Javascript code
function myFunction()
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
var email = document.getElementById("email").value;
var remail = document.getElementById("remail").value;
var password = document.getElementById("password").value;
var status = false;
var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]2,4$/i;
var x = document.getElementById("male").checked;
var y = document.getElementById("female").checked;
var birthday = document.getElementById("bday").value;
if(fname=="")
alert("Please Enter First Name");
else if(lname=="")
alert("Please Enter Last Name");
else if(email=="")
alert("Please Enter Email");
else if (document.myform.email.value.search(emailRegEx) == -1)
alert("Please enter a valid email address.");
else if(remail=="")
alert("Please Enter Re-enter Email");
else if(email != remail)
alert("Please Check Email");
else if(password=="")
alert("Please Enter Password");
else if (password.length<8)
alert("Please enter 8 digit password");
else if(!birthday) //To validate if birth date is empty
alert("Please select Birth Date")
else if(submitBday('bday')>16)//To check age greater than 16
alert("Age should be greater than 16");
else if(x!=true && y!=true)
alert("gender");
else
alert("Name = " + fname + "n" + "Last Name = " + lname
+ "n" + "Email = " + email
+ "n" + "Password = " + password
+ "n" + "DOB = " + day1 + "-" + month1 + "-" + year1
);
function submitBday(id)
var Bdate = document.getElementById(id).value;
var Bday = +new Date(Bdate);
var Q4A = ~~ ((Date.now() - Bday) / (31557600000));
return Q4A;
your html code
<form name="myform">
<h1>User Registration Form</h1>
<input type="text" placeholder="First Name" id="fname">
<input type="text" placeholder="Last Name" id="lname"><br><br>
<input type="text" name="email" placeholder="Email" id="email"><br><br>
<input type="text" placeholder="Re-enter Email" id="remail"><br><br>
<input type="password" placeholder="Enter Password" id="password"><br><br>
Birthdate : <input type="date" name="bday" id="bday"">
reference http://jsfiddle.net/j08691/exVh2/1/
edited Mar 28 at 5:50
answered Mar 28 at 4:10
Deepak ADeepak A
1,5241 gold badge2 silver badges15 bronze badges
1,5241 gold badge2 silver badges15 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389890%2fhow-to-validate-dob-from-dropdown-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown