Click here to Skip to main content
15,867,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I need an example in which the user is only able to write numeric values in a text field.

Actually, I have one field called PhoneNumber where the user can write numbers. But he is also able to write characters. The user should not be able to write characters in the phone number text field.

Thanks!
Posted
Updated 13-May-13 22:49pm
v2

Hi...

validate textbox with RegularExpressionValidator like blow.
its allow only numeric values only into textbox.

XML
<asp:RegularExpressionValidator ID="rxv1" runat="server"
                ErrorMessage="Pls enter Numerics Only" EnableClientScript="true"
             ControlToValidate="txtbox" ValidationExpression="\d+"></asp:RegularExpressionValidator>


see may its helpful to u.
thank u.
 
Share this answer
 
You could use masked input, like this: http://digitalbush.com/projects/masked-input-plugin/[^].
And don't forget to validate on server side too!
 
Share this answer
 
Check beow link for jQuery plugin for phone number masked textbox and

http://digitalbush.com/projects/masked-input-plugin/[^]
 
Share this answer
 
Here is another solution for you.

XML
<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
      //-->
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
   </BODY>
</HTML>


I found it here : http://www.cambiaresearch.com/articles/39/how-can-i-use-javascript-to-allow-only-numbers-to-be-entered-in-a-textbox[^]
 
Share this answer
 
//Javascript code
JavaScript
function ValidateInteger(text) {
    var strString = text.value
    var strValidChars = "0123456789";
    var strChar;
    for (i = 0; i < strString.length; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            text.value = text.value.substring(0, i);
            return false;
        }
    }
}

XML
//use it:
<asp:TextBox ID="txtbox1" runat="server" onkeyup="ValidateInteger(this);"></asp:TextBox>
 
Share this answer
 
Comments
CHill60 26-Jul-14 9:05am    
"Numeric" does not imply "Integer only". Why not use NaN()?

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