Link to home
Start Free TrialLog in
Avatar of erin027
erin027Flag for United States of America

asked on

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

Hello,
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");
            o.birth_month.focus();
            return false;
            }      
// Validating for numeric input using Javascript      
      if (isNaN(o.a)) {
      alert("Please input Number in Birth Date");
       o.birth_date.focus();
      return false;
      }
      
      o.action="new_member_02_ok.asp";
      o.submit();
}

<html>
<head></head>
<body>
<table>
            <tr>
              <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>
              </select>
-
<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"/>
              </td>
            </tr>
</table>

</body>
</html>
Avatar of mrichmon
mrichmon

Where is your function onlyNUmber()?

I only see sendit
ASKER CERTIFIED SOLUTION
Avatar of rlbalan
rlbalan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of b0lsc0tt
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.

bol
just an optional, you can try
http://www.softcomplex.com/products/tigra_form_validator/
there's a field that can validate the correct num to input.

rgds
eugene
Avatar of erin027

ASKER

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() {
      if((event.keyCode<48)||(event.KeyCode>57))
      event.returnValue=false;
}