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!
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!
Why not use <select> instead?
HTML Code:<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>
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
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
Use as template for form:Code:<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; } }
Not tested, but should work. Hope this helps.Code:<form action="something.php" method="POST" onsubmit="return doCheck('number');"> <input type="text" name="something" id="number"> <input type="submit" value="Check Form"> </form>
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design
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)
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!.
Last edited by nicksalad; 06-22-2007 at 05:38 PM.
Bookmarks