Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET validation
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 5-Sep-12 1:08am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try this
<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>
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hello,
 
refer below solution,
 
<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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

Try this:
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[^]
  Permalink  
Comments
kamalsekhar at 29-Mar-14 10:46am
   
hi,
In aspx page how to call this function isNumberKey ??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

<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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hi
 
Try like this
 
bool Valid = false;
 
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
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;
        }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

The NumericTextBox for ASP.NET[^] is what you need. right ?
 
or you can check at this[^]!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Hi....
 
u can set maxlength of ur textboxes as 5 & 4 respectively, and use these validators:
 
<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" />
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150302.1 | Last Updated 5 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100