View Full Version : <input type="text" to accept numbers only, its possible?
nicksalad
06-22-2007, 03:48 PM
Hello, as the title says i want to have on a form a "text" field to accept only numbers (0-9) is this possible? i hope someone could answer me fast, i need to finish it today o.0 Thanks!
jc_gmk
06-22-2007, 03:58 PM
Why not use <select> instead?
<form>
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</form>
nicksalad
06-22-2007, 04:21 PM
Hello, thanks for your fast repy, i can't use select because the number values are from 0 till 99999, its a points adder (calculator machine) and it would make the code real heavy, can you imagine 99999 of those <option? hehe
thetestingsite
06-22-2007, 04:40 PM
You could use javascript to see if a number has been entered in the textbox. If you go this route though; and you are pointing the form action to a server-side script, make sure you have a backup test to see if the data submitted was a number or if it was text (just in case the user has javascript disabled for whatever reason).
Anyways; as for the code for this "check", I think you could do something like the following:
Place between head tags
<script type="text/javascript">
function doCheck(field) {
if (isNaN(document.getElementById(field).value)) {
alert('This is not a number! Please enter a valid number before submitting the form.');
document.getElementById(field).focus();
document.getElementById(field).select();
return false;
}
else {
return true;
}
}
Use as template for form:
<form action="something.php" method="POST" onsubmit="return doCheck('number');">
<input type="text" name="something" id="number">
<input type="submit" value="Check Form">
</form>
Not tested, but should work. Hope this helps.
nicksalad
06-22-2007, 04:54 PM
Hello, when the user inserts a letter(s) instead of numbers and press submit, it gives the congratulations message like if the script were ran correctly but in fact nothing were added into the db, i want o change the message without using javascript if possible, now i was cheching this function but i can't make it work.
$str = (int)str_replace("'","",stripslashes($_POST[strength]));
if (!ereg("([0-9]+)",$str)) { print_error("Only numbers are allowed."); }
It doesn't work and i can't see whats wrong (im noob)
nicksalad
06-22-2007, 05:21 PM
nevermind i found the solution myself:
$str = $_POST[strength];
if ( eregi( "[a-z]", $str ) ) { print_error("Numbers only!."); }
now the problem is with the wierd symbols...
EDIT: WHOAH! PROBLEM FIXED! :]
if ( !eregi( "^[0-9]+$", $str ))
so if they write anything except numbers it will show the error and wont try to save on db. hehe, thanks you two for your reply!.
Powered by vBulletin® Version 4.2.2 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.