-3

i have a input field "age/date of birth". how can i validate the field,if i can enter both age or date.

<tr>
<td>Date of birth / age:<span style="color:#ff0000">*</span><div id="dob_id" style="display:none;">Please Enter Valid Date</div></td>
<td><input class="formField" name="dob" value="" /></td>

i used the following code to validate date.

function dobcheck()
        {               
            var x = document.getElementById("dob_id");
            x.style.display="none";
            var currentTime = new Date()
            var month = currentTime.getMonth() + 1
            var day = currentTime.getDate()
            var year = currentTime.getFullYear()
            if(day<10){day='0'+day} 
            if(month<10){month='0'+month}
            var today = day + "/" + month + "/" + year;
            var ip=document.getElementsByName("dob")[0].value;
            if(ip>=today)
            {
                x.style.display="block";
                x.style.color="red";                    
            }


        }

date validation is working properly.but i need to validate dob or age depends upon the input.

1
  • 2
    Then What you have tried....any code for us
    – GautamD31
    Jan 28, 2013 at 12:30

1 Answer 1

0

This should work for your HTML :

function dobcheck()
{  
    var date_separator = '/'; // Assign your date separator here.
    var me = document.getElementsByClassName('formField')[0].value.replace(/^\s|\s$/g, '');

    if(me.match(/[^0-9]/g) == null)
    {
        // Age is entered.
        alert('My age is '+me);
    }
    else if(me.match(/[^0-9]/g).length == 2)
    {
        // For dates in format dd-mm-yy
        var t = me.split(date_separator);
        if(!isNaN(t[0]) && !isNaN(t[0]) && !isNaN(t[0]) )
        {
            alert('My Date of birth is '+me);
        }
    }
}

Cheers!!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.