By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

date of birth validation

P: 63
i like to validate date of birth as format dd/mm/yyy i tried everything but is not working
thank you
Apr 9 '09 #1
Share this Question
Share on Google+
12 Replies


P: 63
whats wrong with my code
Expand|Select|Wrap|Line Numbers
  1. <!--
  2. function Validate(){
  3. var DOB = new Date(document.forms.DOB.value)
  4. if (DOB == "NaN") {
  5.     alert("DOB required in the format DD/MM/YYYY")
  6.     document.forms.DOB.select.focus()
  7.     return false
  8. }
  9. }
  10. //-->
  11. Date of Birth (dd/mm/yyy): <input type="text" name="DOB" size="30" onblur="Validate()" /></p>
Apr 9 '09 #2

dmjpro
100+
P: 2,476
Show us your code what you tried so far?
Apr 9 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
you test whether the Date object is a string, I doubt it will fire. possibly isNAN() or typeof DOB will get you further
Apr 9 '09 #4

P: 63
i used the above code but it didnt work i dont why
i also i want to validate the the radio buttons so i know if user answered both of the questions
Expand|Select|Wrap|Line Numbers
  1. function checkForm() {
  2. //This fuction shows that the user need to answer Question one
  3.  
  4. var el = document.forms[0].elements;
  5. for(var i = 0 ; i < el.length ; ++i) {
  6. if(el[i].name == "visites") {
  7. var radiogroup = el[el[i].name];
  8. var itemchecked = false;
  9. for(var j = 0 ; j < radiogroup.length ; ++j) {
  10. if(radiogroup[j].checked) {
  11. itemchecked = true;
  12. break;
  13. }
  14. }
  15. if(!itemchecked) {
  16. alert("Please Answer Question One");
  17. if(el[i].focus)
  18. el[i].focus();
  19. return false;
  20. }
  21. }
  22. }
  23. return true;
  24. }
i used this twice but i dont know how
Apr 9 '09 #5

dmjpro
100+
P: 2,476
@lolodede
Actually "document.forms" returns Form Collection then how does it work "document.forms.DOB"?

I think when a String is compared with an Object then Object must be converted into String.
Apr 9 '09 #6

gits
Expert Mod 5K+
P: 5,390
to your first question: you could use a simple regExp to check the form's value for the correct format:

Expand|Select|Wrap|Line Numbers
  1. var s  = '01/01/2009';
  2. var re = /\d{2}\/\d{2}\/\d{4}/;
  3.  
  4. alert(re.test(s))
kind regards
Apr 9 '09 #7

acoder
Expert Mod 15k+
P: 16,027
Don't forget the start and end characters:
Expand|Select|Wrap|Line Numbers
  1. var re = /^\d{2}\/\d{2}\/\d{4}$/;
Apr 9 '09 #8

gits
Expert Mod 5K+
P: 5,390
:) ... yep ... thanks for pointing that out ...

kind regards
Apr 9 '09 #9

P: 63
i used reg expression but is not working right i try to alert it but icant do that i dont know why
thanks for help
Apr 9 '09 #10

gits
Expert Mod 5K+
P: 5,390
show the code that you have tried.

kind regards
Apr 9 '09 #11

dmjpro
100+
P: 2,476
Hello all ...What would be my answer ? ;)
Apr 10 '09 #12

P: 63
thanks for help its working now
Apr 12 '09 #13

Post your reply

Sign in to post your reply or Sign up for a free account.