How to only allow numbers or text in a form

  • Banjer
  • Novice
  • Novice
  • Banjer
  • Posts: 21

Post 3+ Months Ago

How do you only allow either text or numbers in an input field? I want to make a birthday field where you can only input numbers, i use a .php file so it's not limited to html, could someone tell me how to do this?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

http://us4.php.net/manual/en/function.is-numeric.php
  • Banjer
  • Novice
  • Novice
  • Banjer
  • Posts: 21

Post 3+ Months Ago

Too complicated for me =/
  • Rat
  • Guru
  • Guru
  • User avatar
  • Posts: 1190
  • Loc: desk

Post 3+ Months Ago

Code: [ Select ]
<form name="checknum" onSubmit="return checkban()">
Please enter your age:<br>
<input type="text" name="pnum">
<input type="submit" value="Submit">
</form>

<script language="JavaScript1.2">

function checknumber(){
var x=document.checknum.pnum.value
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
alert("Please input a valid number!")
testresult=false
}
return (testresult)
}

</script>
<script>
function checkban(){
if (document.layers||document.all||document.getElementById)
return checknumber()
else
return true
}
</script>
  1. <form name="checknum" onSubmit="return checkban()">
  2. Please enter your age:<br>
  3. <input type="text" name="pnum">
  4. <input type="submit" value="Submit">
  5. </form>
  6. <script language="JavaScript1.2">
  7. function checknumber(){
  8. var x=document.checknum.pnum.value
  9. var anum=/(^\d+$)|(^\d+\.\d+$)/
  10. if (anum.test(x))
  11. testresult=true
  12. else{
  13. alert("Please input a valid number!")
  14. testresult=false
  15. }
  16. return (testresult)
  17. }
  18. </script>
  19. <script>
  20. function checkban(){
  21. if (document.layers||document.all||document.getElementById)
  22. return checknumber()
  23. else
  24. return true
  25. }
  26. </script>



Simply cut and paste the below code into the <body> section of your page. It
contains a form with one box that is checked for "number validity". You can add in more form elements into the form, as you would with any other form.
  • Bob7
  • Banned
  • Banned
  • Bob7
  • Posts: 4

Post 3+ Months Ago

Why the hell would you use all that when you can use is_numeric($var); as joebert said?
  • Rat
  • Guru
  • Guru
  • User avatar
  • Posts: 1190
  • Loc: desk

Post 3+ Months Ago

Banjer wrote:
Too complicated for me =/

i rest my case
  • Bob7
  • Banned
  • Banned
  • Bob7
  • Posts: 4

Post 3+ Months Ago

Which is more complicated, a couple characters.. or a full page of slow ass javascript code? How is spewing a full page of horrible code less complicated than 1 line?

He can't follow 2 simple words... so you throw a full page of *plum* at him... brilliant.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Rats post works too :D

If your just preventing "clumsy fingers" & people who don't pay attention while filling out forms javascript is the way to go.

If you need to make sure certain chars CAN'T be passed, server side is the way to go :D

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 32 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.