How to validate only numbers on the form by using Javascript?

Posted on 2007-10-01
Last Modified: 2013-11-19
Below are my javascript and form code.
I am trying to alert users not to enter characters (Allow only numbers) by using javascript.
What did I do wrong?

<script language="javascript" src="../function/trim.js"></script>
<script type="text/javascript">
function sendit(o) {
            if (Trim(o.birth_month.value).length<2) {
            alert ("Please fill out Birth Month");
            return false;
// Validating for numeric input using Javascript      
      if (isNaN(o.a)) {
      alert("Please input Number in Birth Date");
      return false;

              <td class="stylePaddingleftright55" style="padding-bottom:12px;">
              <select size="1" name="birth_month">
                <option value="">Month</option>
                <option value="01">January</option>
                <option value="02">February</option>
                <option value="03">March</option>
                <option value="04">April</option>
                <option value="05">May</option>
                <option value="06">June</option>
                <option value="07">July</option>
                <option value="08">August</option>
                <option value="09">September</option>
                <option value="10">October</option>
                <option value="11">November</option>
                <option value="12">December</option>
<input name="birth_date" type="text" value="dd" size="2" maxlength="2" onKeyPress="onlyNumber();" onFocus="this.value=''" autocomplete="off"/>
<input name="birth_year" type="text" value="yyyy" size="4" maxlength="4" onKeyPress="onlyNumber();" onFocus="this.value=''" autocomplete="off"/>

Question by:erin027
    LVL 35

    Expert Comment

    Where is your function onlyNUmber()?

    I only see sendit
    LVL 3

    Accepted Solution

    use  if (isNaN(parseInt(o.a)))  {..}

    I dont see the onlyNumber() script, but I assume, it does somethng like the above.

    Check example here :

    // JavaScript Function
    function AllowOnlyNumeric()
        // Get the ASCII value of the key that the user entered
        var key = window.event.keyCode;

        // Verify if the key entered was a numeric character (0-9) or a decimal (.)
        if ( (key > 47 && key < 58) || key == 46 )
            // If it was, then allow the entry to continue
            // If it was not, then dispose the key and continue with entry
            window.event.returnValue = null;

    // HTML Code
    <input type="text" name="[ControlName]" onkeypress="AllowOnlyNumeric()" />
    LVL 54

    Expert Comment

    Using keyCode will only work with IE.  The function below should work in most browsers.

    function onlyNumber(e) {
        var k = (e.keyCode)? e.KeyCode : e.which;
        if (k < 48 || k > 57) {
            alert('Please only type a number.');
            return false;

    You will need to modify the function call.

    <input name="birth_year" type="text" value="yyyy" size="4" maxlength="4" onKeyPress="onlyNumber(event);" onFocus="this.value=''" autocomplete="off"/>

    Let me know if you have a question or need more info.

    LVL 3

    Expert Comment

    just an optional, you can try
    there's a field that can validate the correct num to input.


    Author Comment

    Sorry, My bad.
    I couldn't understand the Javascript, because i never actually learned js, but I figured it out.
    It should've have this:

    function onlyNumber() {


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now