Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a a web form that has text box called 'txtEmpNo'. At that text box I only want to enter numbers. In other hand I want to restrict the content to numbers when enter A - Z that text box should not allow to do that!

I use C#.Net with ASP.Net

How am going to solve that? I already seen the CustomValidation control in ASP.Net but I do not know how to validate the content of a text box in ASP.Net to validate using it!

If you can please help me to do that!

Thanks
Chiranthaka
Posted
Comments
Bikash Prakash Dash 9-May-13 0:49am    
try Solution 2 my friend

Try using the Masked Edit Control[^] from the ASP.NET AJAX Control Toolkit.
 
Share this answer
 
Comments
Richard C Bishop 8-May-13 17:43pm    
That is a good idea. I did not even know of it. +5.
Sergey Alexandrovich Kryukov 8-May-13 17:53pm    
Makes sense, a 5.
—SA
Use a RegularExpression validator and use this: ^[0-9]{1,7}$ as your regular expression. Then you can change the error message to whatever you want.
 
Share this answer
 
v3
Comments
Chiranthaka Sampath 8-May-13 17:45pm    
I have used that and gave me a run time error! Saying

The value '' of the ValueToCompare property of 'cmvEmpNo' cannot be converted to type 'Integer'.

Then How to do that!
Richard C Bishop 8-May-13 17:47pm    
See my updated solution please.
Chiranthaka Sampath 8-May-13 17:54pm    
I only set those 2 properties only pal! What I have done wrong ?
Chiranthaka Sampath 8-May-13 18:12pm    
Yes I have use that RegularExpression validator. I add the value for the 'Validation Expression' property with your '^[0-9]{1,7}$' and there is no any property as 'Regular Expression'. How ever it gives me a list of files in my projects' folder instead of validating for numbers! I don't know how to solve this!
Richard C Bishop 9-May-13 9:47am    
Can you post the code of the control and validtor? That may help us to help you.
you should try it,
register it on page-
]]>
then use it. i think your problem will be solved.
<cc1:filteredtextboxextender id="ffStanderd" runat="server" validchars="0123456789." xmlns:cc1="#unknown">
TargetControlID="txtEmpNo">
 
Share this answer
 
Use Masked edit Ajax Toolkit, go through this link , you'll know how to use it
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/MaskedEdit/MaskedEdit.aspx[^]
 
Share this answer
 
Try This ...
<script type="text/javascript">
    function isNumberic(evt) {
      var charCode = (evt.which) ? evt.which : event.keyCode
      if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

      return true;
    }
</script>

------------------------------------------------------------------------------------
<asp:textbox id="txtEmpNo" runat="server" onkeypress="return isNumberic(event)">
</asp:textbox>
 
Share this answer
 
v3
Comments
Chiranthaka Sampath 12-May-13 1:59am    
I need a server side solution rather than JavaScript or JQuery or AJAX because when the browser's JavaScript is disabled the validation is not going to work!

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