Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
heyyy ...I have to validate the numeric and dot and backspace also validate single dot enter...(ie. enter dot(.) only without entering other digits)

but my code is allowed '.' dot ...here is my code..and tell how can I validate the dot(.) in textbox

XML
<asp:TextBox ID="textPaid" runat="server" onkeypress="return isNumberKeynew(event)"
                                            oncopy="return false" onpaste="return false" oncut="return false" ondragstart="return false" ondrop="return false" AutoComplete="off" attr-identity="PaidAmount"
                                         MaxLength="7" CssClass="textboxForBill1"></asp:TextBox>






C#
function isNumberKeynew(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode;
         if (charCode != 46 && charCode > 31
           && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
Posted
Updated 23-Jan-13 8:04am
v3
Comments
Member 9644686 23-Jan-13 6:09am    
can u tell me u want to allows dot,backspace,numeric in ur textbox not anothers
is it write.
aravindnass 23-Jan-13 6:23am    
yes...the above javascript is to allow this ...but my problem is the javascript allows single . (dot)..(so textbox accepts single '.' without entering any values)

Eg: 12.22 - valid
. - invalid
[no name] 23-Jan-13 7:30am    
use regular expresn or just read input of textbox along with event and throw alert accordingly..
aravindnass 23-Jan-13 10:52am    
can u give me regular expression for this

1 solution

try this code.....
XML
<asp:TextBox ID="textPaid" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="regexpName" runat="server"
                                    ErrorMessage="This expression does not validate."
                                    ControlToValidate="textPaid"
                                    ValidationExpression="^[0-9]*\.?[0-9]*$" />


also go through following link's for custom regular expression validator....
http://msdn.microsoft.com/en-us/library/ff650303.aspx[^]
http://msdn.microsoft.com/en-us/library/ms972966.aspx[^]
Validation with Regular Expressions Made Simple[^]
 
Share this answer
 
Comments
aravindnass 24-Jan-13 3:06am    
but it also allowed single . without entering any digits....
Pallavi Waikar 24-Jan-13 9:18am    
is this validation is for decimal value...then only u have to do is remove ? from expression...... ? mean's dot is may required or not in i/p...i.e it allow both single and decimal values..use "^[0-9]*\.[0-9]*$" .....in ValidationExpression

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