Hey there! Sign in to join this conversationNew here? Join for free
    • Thread Starter
    Offline

    1
    ReputationRep:
    Basically I need help with an age validation for JavaScript and Html , here is the question

    "Add a textbox for user to enter his/her age. Your program should validate user for age. Assume that a valid age is between 0 and 150"

    how would I do this on HTML Rather than JavaScript ?


    email address is [email protected]
    Offline

    0
    ReputationRep:
    You can't do logic with HTML - you have to use JavaScript (And server-side verification) for this anyway.
    • Thread Starter
    Offline

    1
    ReputationRep:
    (Original post by laser)
    You can't do logic with HTML - you have to use JavaScript (And server-side verification) for this anyway.
    im sorry i got confused i meant js
    Offline

    2
    ReputationRep:
    Code:
    <html>
    
    <form name="form">
    Age: <input type="text" name="age" maxlength="3" onKeydown="return validateNumber(event)" />
    <input type="submit" value="Submit" onClick="return validateAge()"/>
    </form>
    
    <script type="text/javascript">
    
    function validateNumber(e)
    {
    	if(e.keyCode < 48 || e.keyCode > 57)
    	{
    		return false;
    	}
    }
    
    function validateAge()
    {
    	if(form.age.value > 150)
    	{
    		alert("Please enter a valid age.");
    		return false;
    	}
    }
    
    </script>
    
    </html>
    Is this what you mean?

    Seems like a stupid question anyways, all websites use dropdown boxes for the user to select their age.
 
 
 
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • Poll
    Will you be richer or poorer than your parents?
  • See more of what you like on The Student Room

    You can personalise what you see on TSR. Tell us a little about yourself to get started.

  • The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

    Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

    Quick reply
    Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.