How to only allow numbers or text in a form

  • Banjer
  • Novice
  • Novice
  • No Avatar
  • Joined: Jul 14, 2004
  • Posts: 21
  • Status: Offline

Post July 31st, 2004, 3:07 pm

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 31st, 2004, 3:07 pm

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post July 31st, 2004, 4:17 pm

http://us4.php.net/manual/en/function.is-numeric.php
Strong with this one, the sudo is.
  • Banjer
  • Novice
  • Novice
  • No Avatar
  • Joined: Jul 14, 2004
  • Posts: 21
  • Status: Offline

Post August 1st, 2004, 4:50 am

Too complicated for me =/
  • Rat
  • Guru
  • Guru
  • User avatar
  • Joined: Apr 25, 2004
  • Posts: 1190
  • Loc: desk
  • Status: Offline

Post August 1st, 2004, 5:16 am

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
  • No Avatar
  • Joined: Aug 01, 2004
  • Posts: 4
  • Status: Offline

Post August 1st, 2004, 5:18 am

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

Post August 1st, 2004, 5:23 am

Banjer wrote:
Too complicated for me =/

i rest my case
  • Bob7
  • Banned
  • Banned
  • No Avatar
  • Joined: Aug 01, 2004
  • Posts: 4
  • Status: Offline

Post August 1st, 2004, 5:25 am

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
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post August 1st, 2004, 8:04 am

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
Strong with this one, the sudo is.

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 98 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
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.