Solved

only numbers allowed

Posted on 2007-11-30
17
515 Views
Last Modified: 2012-06-27
<input type="text" onKeyUp="if (isNaN(this.value)) this.value=this.value.replace(/[^0-9.]/g,'')">
this do the work partly cause it leaves the point...
0
Comment
Question by:sasha85
  • 6
  • 6
  • 3
  • +1
17 Comments
 
LVL 19

Expert Comment

by:erikTsomik
ID: 20387093
0
 

Author Comment

by:sasha85
ID: 20387100
"no point"
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20387106
What is the problem?  The solution is from the question ErikTsomik pointed to.  Is there a problem?

What character are you putting in that is getting past the script or causing some other problem?

If you don't want to allow the decimal point then just remove it and use ...

<input type="text" onKeyUp="if (isNaN(this.value)) this.value=this.value.replace(/[^0-9]/g,'')">

Let me know if you have a question or need more info.

bol
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:sasha85
ID: 20387119
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 20387122
that it supposed to be because it displays money value and it can contains pennies
0
 

Author Comment

by:sasha85
ID: 20387137
i need to display/insert 10 numbers...that why i do not need this point:)
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20387141
Do you need to test for 10 numbers?  If you just want to remove the period and all other non-numeric characters then use the html and script I modified.  It will do that.

bol
0
 
LVL 19

Expert Comment

by:erikTsomik
ID: 20387142
input type="text" onKeyUp="if (isNaN(this.value)) this.value=this.value.replace(/[^0-9]/g,'')">
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20387156
erikTsomik,

Did you notice http:#20387106 ?  Your post copies the contents of mine.  Please either give credit or read previous comments more carefully.  Hopefully it was just a mistake but it always best to be careful so that type of thing isn't done even accidentally. :)

Let me know if you have a question about this.

bol
0
 

Author Comment

by:sasha85
ID: 20387209
i don ;t know where i got wrong with explaining my question but i will try again:)
i need that inside the input text user will be able to insert only numbers 0-9, nothing else.
yes it have to be 10...but this i will do with maxlenght...

onKeyUp="if (isNaN(this.value)) this.value=this.value.replace(/[^0-9]/g,'')
this not good cause it premit the user to write point between the numbers like it be a price...than its not good
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20387223
The problem is the isNaN.  use ...

<input type="text" onkeypress="var k = (e.which)? e.which : e.keyCode; return (k < 48 || k > 57) ? false : return true;">

That should do it.  Sorry for overlooking the isNaN.

bol
0
 

Author Comment

by:sasha85
ID: 20387239
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 350 total points
ID: 20387254
There was a few typos.  One that caused a big problem.  This works.

<input type="text" onkeypress="var k=(event.which)? event.which : event.keyCode; return (k < 48 || k > 57) ? false : true;" />

bol
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20388432
You mean
<input type="text" onkeypress="var k = (e.which)? e.which : e.keyCode; return (k < 48 || k > 57) ? false : true;">

or

<input type="text" onkeypress="var k = (e.which)? e.which : e.keyCode; return !(k < 48 || k > 57) ">

or

<input type="text" onKeyUp="this.value=this.value.replace(/[^0-9]/g,'')">
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 20388436
Oh, You fixed it
0
 

Author Comment

by:sasha85
ID: 20388770
sorry, what?:)
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20389740
Thanks for the grade, the points and the fun question.

bol

p.s. mplungjan, Yeah, I caught that error and also had to change the code so e was actually defined.  The script is usually in a function but I knew Sasha85 wanted it short as possible and probably inline.  Thanks for posting. :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question