aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Only Numeric value allowed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Only Numeric value allowed" Watch "Only Numeric value allowed" New topic
Author

Only Numeric value allowed

Mahesh Mamani
Ranch Hand

Joined: Jun 25, 2001
Posts: 110
Hi,
Hello I have a text field. Would like to use the user to key in only numeric values. Is there any property in the text field, which sets this. Don't wanna use JavaScript. If no other option then no other go, but to use it.
Thanks in advance,

Mahesh
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

im afraid not. you will have to use scripting.


SCJP
Visit my download page
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
this is really ugly but a select with options can be used to limit input values.The javascript function is only for demonstration.
Mahesh Mamani
Ranch Hand

Joined: Jun 25, 2001
Posts: 110
Hi,
Thanks for Ur help.
In WML there is a property called FORMAT for text field in which we can specify the format reqd(numeric, alphanumeric, and so on)...
SO, is there no such property in HTML.

Mahesh
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
I agree the input type=text control should have such a std attribute as format.X-Forms will but that is a ways off.
Handy Online Guides including html:
http://www.devguru.com/Technologies/html/quickref/html_input.html#text
Mahesh Mamani
Ranch Hand

Joined: Jun 25, 2001
Posts: 110
Thanks Guys,
I think the only option is using JavaScript.

Mahesh
Charlie Sturman
Ranch Hand

Joined: Apr 04, 2002
Posts: 112
Here is a numericOnly Format option for input text controls, just add onkeypress="formatNumberKp()" in the input type=text tag and include the 2 simple functions.With a little work you could extend this to any browser and any format.

[ April 30, 2002: Message edited by: Charlie Sturman ]
Rob Hunter
Ranch Hand

Joined: Apr 09, 2002
Posts: 823
Mahesh,
Are you just validating the input for numbers or do you want to allow for number selection only, like suggested with the drop downs? If just validating, using regular expressions is a good choice.
Rob
A GPuvathingal
Greenhorn

Joined: May 23, 2002
Posts: 10
Hi Mahesh,
Though this comes late -- you could try this code .....
<script>
function onlyNumeric(){
if( (event.srcElement.type == "text") && isNaN(event.srcElement.value) )
{
alert("Enter a numeric value")
str = event.srcElement.value
truncatedValue = str.substring(0,str.length-1)

if(isNaN(truncatedValue)==false)
event.srcElement.value =truncatedValue
else
event.srcElement.value =""
}
}
document.onkeyup = onlyNumeric ;
</script>
This code will make sure every text box in the page allows only numeric values.
All the best....


-
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Only Numeric value allowed