Click here to Skip to main content
15,918,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
allow only one float point in a Text Box in Asp.Net. how to write java script function?
Example
A User Enter 12..3 or 15.2. or ..54 this time Validate..So how to Validate used in JavaScript..Any Examples?Help Me...
-------
in My View User Only Enter one decimal Point between the number
Posted
Updated 13-Jun-11 2:38am
v2
Comments
Sergey Alexandrovich Kryukov 13-Jun-11 23:28pm    
Javascript, not "Java script" -- important to avoid confusion with Java.
--SA

XML
you can achieve your requirement by using combination of Javascript function & ajax FilteredTextBoxExtender.

Add following java script function in your .aspx file

<script type="text/javascript">

function IsOneDecimalPoint(evt) {
                var charCode = (evt.which) ? evt.which : event.keyCode; // restrict user to type only one . point in number
                var parts = evt.srcElement.value.split('.');
                if(parts.length > 1 && charCode==46)
                    return false;
                return true;
            }
</script>

Write following tag for textbox & ajax FilteredTextBoxExtender.

<asp:TextBox ID="TextBoxValue" runat="server" onkeypress="return IsOneDecimalPoint(event);">
<ajax:FilteredTextBoxExtender ID="FilteredTextBoxExtender" runat="server" FilterType="Custom"
  ValidChars="01234567890." TargetControlID="TextBoxValue"></ajax:FilteredTextBoxExtender>

-Add other tags as per your requirments..
- Now user can only type o to 9 & only one decimal point(.)
 
Share this answer
 
Comments
PaulMD 24-Feb-14 11:15am    
Hi,
Sorry to resurrect an old forum,

I have the above solution working fine(IsOneDecimalPoint(evt))

Would it be possible to allow an extra character?
Say I want to allow only 1 decimal but also a minus character (for negative values)

Thanks,
Paul
Try this

JavaScript - One and Only One[^]

Also you can use this same function for email validation (The @ symbol)
 
Share this answer
 
You can use custome validator for this instead of Java Script.
 
Share this answer
 
Comments
Mohammed Asif.K 14-Jun-11 1:19am    
if u don't mind ,give an example?
Here is JavaScript..
function Validate(form) {

	var TB = form.TextBox1.Value

	Var str = TB.split(/\./);

	if(str.length>2) 
		return false;

	return true;
}

And TextBox
<asp:textbox id="TextBox1" runat="server" onkeyup="Validate(this.form)></asp:TextBox>
 
Share this answer
 
v2
Check it Out[^]


Hope it helps
 
Share this answer
 
Here's a javascript function you can use

XML
<script>
function checkDec(el){
 var ex = /^[0-9]+\.?[0-9]*$/;
 if(ex.test(el.value)==false){
    alert('Incorrect Number');
  }
}
</script>


To use it, call it in the onblur event
Eg: in html
<input type="text"  önblur="checkDec(this);" />

Eg : in Asp.Net
<asp:TextBox onblur="checkDec(this);" ...


Note : i've made the decimal number entry optional here. If you want to make it compulsory, just take of the '?' in the line beginning with var ex =

Also the first character cannot be decimal here (for safety). Must enter 0.xxx instead of beginning with a .

(put the <spript></script> block in the <head> section)
 
Share this answer
 
v3
Comments
Mohammed Asif.K 14-Jun-11 2:59am    
thanks for your valuable Code Snippet
Your .Net validation should include a call to double.TryParse(). If it returns false, the value could not be converted to a double (and a two decimal points would cause it to return false).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Jun-11 23:29pm    
Sure, a 5.
It's very hard to do correct validation without this call.
--Sa

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900