Link to home
Start Free TrialLog in
Avatar of kwebster7327
kwebster7327

asked on

Limit a Textbox to Numeric Only with CSS

Given a plain old asp textbox, is there a way to only allow numeric input using CSS. Can't find it in the book, but this seems like it should be obvious.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Not with CSS, but with javascript you could.
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
 
   if (strString.length == 0) return false;
 
   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

Open in new window

SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kwebster7327
kwebster7327

ASKER

Kaufmed: almost a winner until I saw Zvonko's minimalist approach.
How is that an accepted solution when the code is broken? Okay... a slight nitpick... but here is a corrected version with the size options included.

<input type="text" name="Zipcode" onKeyUp="if(this.value.match(/\D/))this.value=this.value.replace(/\D/g,'')" size="10" maxlength="5" />
There are tons of open questions where you can show how expert you are and you do here unsolicited posts? Where do you read here Zip was required except in your comment???
And you copied my code without to reference that it was MY solution!

Now you either apologise for your wrong extension or you show where my code was broken.
Zvonko: Whoa there! Calm down... Perhaps I should have put a smiley face on my response. But I am sorry if I offended you.

What I meant was the error was in missing the open quote before text. I had actually written more after the corrected code but, for some darn reason the rest of my message is not there.

You had wrote: [<input type=text"....]
When it should be [<input type="text"....]

BTW, this is a variation of the method I use but kudos to you. Your code contribution is more elegant than mine. That was something else that should have been in my response.

Can we live in peace now? :-)
Uhps, that missing quote character I have not noticed. You are right, my apologies to you <|;-)
No prob. my original posting was misleading. I should have read it after I posted it.... then I would have noticed that 1/2 of it was missing. Take care! I'm going to sleep now. :-)