date of birth validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lolodede
    New Member
    • Apr 2009
    • 63

    date of birth validation

    i like to validate date of birth as format dd/mm/yyy i tried everything but is not working
    thank you
  • lolodede
    New Member
    • Apr 2009
    • 63

    #2
    whats wrong with my code
    Code:
    <!--
    function Validate(){
    var DOB = new Date(document.forms.DOB.value)
    if (DOB == "NaN") {
        alert("DOB required in the format DD/MM/YYYY")
        document.forms.DOB.select.focus()
        return false
    }
    }
    //-->
    Date of Birth (dd/mm/yyy): <input type="text" name="DOB" size="30" onblur="Validate()" /></p>
    Last edited by Dormilich; Apr 9 '09, 05:43 AM. Reason: added [code] tags

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Show us your code what you tried so far?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you test whether the Date object is a string, I doubt it will fire. possibly isNAN() or typeof DOB will get you further

        Comment

        • lolodede
          New Member
          • Apr 2009
          • 63

          #5
          date birth validation

          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
          Code:
          function checkForm() {
          //This fuction shows that the user need to answer Question one
          
          var el = document.forms[0].elements;
          for(var i = 0 ; i < el.length ; ++i) {
          if(el[i].name == "visites") {
          var radiogroup = el[el[i].name];
          var itemchecked = false;
          for(var j = 0 ; j < radiogroup.length ; ++j) {
          if(radiogroup[j].checked) {
          itemchecked = true;
          break;
          }
          }
          if(!itemchecked) {
          alert("Please Answer Question One");
          if(el[i].focus)
          el[i].focus();
          return false;
          }
          }
          }
          return true;
          }
          i used this twice but i dont know how
          Last edited by gits; Apr 9 '09, 06:34 AM. Reason: added code tags

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by lolodede
            whats wrong with my code
            Code:
            <!--
            function Validate(){
            var DOB = new Date(document.forms.DOB.value)
            if (DOB == "NaN") {
                alert("DOB required in the format DD/MM/YYYY")
                document.forms.DOB.select.focus()
                return false
            }
            }
            //-->
            Date of Birth (dd/mm/yyy): <input type="text" name="DOB" size="30" onblur="Validate()" /></p>
            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.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              to your first question: you could use a simple regExp to check the form's value for the correct format:

              [CODE=javascript]
              var s = '01/01/2009';
              var re = /\d{2}\/\d{2}\/\d{4}/;

              alert(re.test(s ))[/CODE]
              kind regards

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Don't forget the start and end characters:
                Code:
                var re = /^\d{2}\/\d{2}\/\d{4}$/;

                Comment

                • gits
                  Recognized Expert Moderator Expert
                  • May 2007
                  • 5390

                  #9
                  :) ... yep ... thanks for pointing that out ...

                  kind regards

                  Comment

                  • lolodede
                    New Member
                    • Apr 2009
                    • 63

                    #10
                    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

                    Comment

                    • gits
                      Recognized Expert Moderator Expert
                      • May 2007
                      • 5390

                      #11
                      show the code that you have tried.

                      kind regards

                      Comment

                      • dmjpro
                        Top Contributor
                        • Jan 2007
                        • 2476

                        #12
                        Hello all ...What would be my answer ? ;)

                        Comment

                        • lolodede
                          New Member
                          • Apr 2009
                          • 63

                          #13
                          thanks for help its working now

                          Comment

                          Working...