erin027
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="stylePaddingleftrig ht55" style="padding-bottom:12px ;">
<select size="1" name="birth_month">
<option value="">Month</option>
<option value="01">January</option >
<option value="02">February</optio n>
<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</opti on>
<option value="10">October</option >
<option value="11">November</optio n>
<option value="12">December</optio n>
</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>
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 type="text/javascript">
<!--
function sendit(o) {
if (Trim(o.birth_month.value)
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
o.submit();
}
<html>
<head></head>
<body>
<table>
<tr>
<td class="stylePaddingleftrig
<select size="1" name="birth_month">
<option value="">Month</option>
<option value="01">January</option
<option value="02">February</optio
<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</opti
<option value="10">October</option
<option value="11">November</optio
<option value="12">December</optio
</select>
-
<input name="birth_date" type="text" value="dd" size="2" maxlength="2" onKeyPress="onlyNumber();"
-
<input name="birth_year" type="text" value="yyyy" size="4" maxlength="4" onKeyPress="onlyNumber();"
</td>
</tr>
</table>
</body>
</html>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(eve nt);" onFocus="this.value=''" autocomplete="off"/>
Let me know if you have a question or need more info.
bol
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(eve
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
http://www.softcomplex.com/products/tigra_form_validator/
there's a field that can validate the correct num to input.
rgds
eugene
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)||(ev ent.KeyCod e>57))
event.returnValue=false;
}
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)||(ev
event.returnValue=false;
}
I only see sendit