Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Dear Friends,

I have two textbox on my webpage

Example as :

Office Telephone No : TEXTBOXOffTel
Extension : TEXTBOXextension

Now my requirement is - in TEXTBOXOfftel user must enter only numbers not alphabets and numbers must be only 5 digits.

and in TEXTBOX extension user must enter only numbers not alphabets and numbers must be only 4 digits.

Please can u help me.

Thanks in advance.
Posted
Updated 8-Oct-17 6:36am

XML
<script type ="text/javascript">
    function validate() {
var digits = "0123456789";
        var temp;
        if (document.getElementById("<%=TEXTBOXOffTel.ClientID %>").value.length > 5){
        alert("cannot be more than 5 charecters ");
                document.getElementById("<%=TEXTBOXOffTel.ClientID%>").focus();
                return false;
                }
                else {

        for (var i = 0; i < document.getElementById("<%=TEXTBOXOffTel.ClientID %>").value.length; i++) {
            temp = document.getElementById("<%=TEXTBOXOffTel.ClientID%>").value.substring(i, i + 1);
            if (digits.indexOf(temp) == -1) {
                alert("cannot be Blank or Character ");
                document.getElementById("<%=TEXTBOXOffTel.ClientID%>").focus();
                return false;
            }
            }
            }


       return true;
    }

</script>


Same you can do with the other textbox
 
Share this answer
 
Try this:
C#
function isNumberKey(evt, obj) 
         {
            var LIMIT = 5;
            var charCode = (evt.which) ? evt.which : event.keyCode
            var txt = obj.value.length;
            if ((txt == LIMIT) && (charCode == 8)) {
                obj.value = obj.value.toString().substring(0, txt-1);
            }
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            else {
                if (txt < LIMIT) {
                    return true;
                }
                else {
                    return false;
                }
            }
        }

Found it: here[^]
 
Share this answer
 
Comments
kamalsekhar 29-Mar-14 10:46am    
hi,
In aspx page how to call this function isNumberKey ??
Hi....

u can set maxlength of ur textboxes as 5 & 4 respectively, and use these validators:

XML
<asp:RangeValidator ID="cvTEXTBOXOffTel" runat="server" ControlToValidate="TEXTBOXOffTel" Type="Integer" MinimumValue="0" MaximumValue="99999" />
    <asp:RangeValidator ID="cvTEXTBOXextension" runat="server" ControlToValidate="TEXTBOXextension" Type="Integer" MinimumValue="0" MaximumValue="9999" />
 
Share this answer
 
Hi
Try like this

C#
bool Valid = false;


C#
private void button1_Click(object sender, EventArgs e)
        {

            Regex rx = new Regex("^[0-9]*$");
            if (!string.IsNullOrEmpty(TxtTelphone.Text))
            {
                if (rx.IsMatch(TxtTelphone.Text))
                {
                    Valid = true;
                }
                else
                {
                    MessageBox.Show("characters not allowed.");
                    TxtTelphone.Clear();
                    TxtTelphone.Focus();
                    return;
                }
            }
            else
            {
           MessageBox.Show("Please enter a Telphone no and click Ok.");
                TxtTelphone.Focus();
                return;
            }
        }


And allow only for 5 digits use this code
C#
private void TxtTelphone_KeyPress(object sender, KeyPressEventArgs e)
        {
           //You may make it 4 or 5 depends on your application
           if (TxtTelphone.Text.IndexOf(' ') == TxtTelphone.Text.Length - 6)
                    e.Handled = true;
        }
 
Share this answer
 
Comments
Member 11889312 27-Aug-15 3:30am    
not working yet
Hello,

refer below solution,

XML
<asp:TextBox ID="txtofficeTel" runat="server" Maxlength="5" 
Width="30px" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpresphone1" ValidationGroup="phone" Display="Dynamic"
ControlToValidate="txtofficeTel" runat="server" ErrorMessage="Enter Valid Phone Number."
SetFocusOnError="True" ValidationExpression="^\d{5}$"></asp:RegularExpressionValidator>
<asp:TextBox ID="txtofficeExtentionbox" runat="server" MaxLength="4"  
Width="30px" ></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpresphone2" ValidationGroup="phone" Display="Dynamic"
ControlToValidate="txtofficeExtentionbox" runat="server" ErrorMessage="Enter Valid Phone Number."
SetFocusOnError="True" ValidationExpression="^\d{4}$"></asp:RegularExpressionValidator>


this will help you
 
Share this answer
 
The NumericTextBox for ASP.NET[^] is what you need. right ?

or you can check at this[^]!
 
Share this answer
 
Try this
C#
<asp:textbox id="txt" runat="server" maxlength="5" ></asp:textbox>
<asp:RegularExpressionValidator ID="rev" runat="server"    ControlToValidate="txt"
  ErrorMessage="Enter correct shop order" 
                                    ValidationExpression="^\d+$"></asp:RegularExpressionValidator>
 
Share this answer
 
v2
 
Share this answer
 

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