[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6032
  • Last Modified:

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>
0
erin027
Asked:
erin027
1 Solution
 
mrichmonCommented:
Where is your function onlyNUmber()?

I only see sendit
0
 
rlbalanCommented:
use  if (isNaN(parseInt(o.a)))  {..}


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

Check example here :
http://www.geekycodesamples.com/article.aspx?id=10006

// 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
        return;
    else
        // 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()" />
0
 
b0lsc0ttCommented:
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
0
 
sonysgCommented:
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
0
 
erin027Author Commented:
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;
}

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now