Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am trying a textbox which accept mobile number if it is greater than ten. Problem is that it also accept the string. can i validate it to accept only digit. here is my code:
<script language="C#" runat="server">
    protected void CustomValidatorPhone(object source, ServerValidateEventArgs args)
  {
      if (txtMobileNumber.Text.Length <=10)
    {  args.IsValid = true; }
   else
    {  args.IsValid = false; }
}</script>

and callling it on:
<asp:CustomValidator CSSClass="body" id="cusPhone" ValidationGroup="EditProfile"   OnServerValidate="CustomValidatorPhone" Text="Invalid mobile Number"   runat="server" /> </div>

so in script what i need to add or extend my code.. thanks in advance..
Posted
Updated 14-Dec-10 23:59pm
v2
Comments
Toniyo Jackson 15-Dec-10 5:59am    
Use code block only for code.

You just give MaxLength=10 to the textbox txtMobileNumber.

You can use RegularExpression[^] or javascript[^]

Do customizations based on your need, that's all.
 
Share this answer
 
v3
Comments
balongi 15-Dec-10 6:02am    
ok , now how it prevent to enter string.
thatraja 15-Dec-10 6:08am    
check my updated answer
For that, you can use regular expressions, that takes onl y numbers.
For details, Have a look
 
Share this answer
 
You can use RegularExpression to find whether the text is number.
Below is the code

System.Text.RegularExpressions.Regex regularExpression = new System.Text.RegularExpressions.Regex("^-[0-9]+$|^[0-9]+$");
           return regularExpression.Match(astrText).Success;
 
Share this answer
 
Try this,
Javascript:
XML
<script type="text/javascript" language="javascript">
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }

<asp:CustomValidator CSSClass="body" id="cusPhone" ValidationGroup="EditProfile"   onkeypress="return isNumberKey(event)" Text="Invalid mobile Number"   runat="server" />
 
Share this answer
 
Comments
fjdiewornncalwe 15-Dec-10 14:38pm    
A good answer. I would suggest that before using the "evt" argument that it be checked for null first. ie. if( !evt ) return; or something like that.
Check this

JavaScript
function isAlphabetic(val1)
{
    if (val1.match(/^[0-9]+$/))
    {
        return true;
    }
    else{
      if (val1.match(/^[A-Z]+$/)){
       return false;
      }
    }
}
 
Share this answer
 
v2
Comments
fjdiewornncalwe 15-Dec-10 14:40pm    
Just fixed your pre block...
XML
<asp:TextBox ID="TextBox1" MaxLength="10" runat="server"></asp:TextBox>
        <asp:RegularExpressionValidator ControlToValidate="TextBox1" ID="RegularExpressionValidator1" runat="server" ErrorMessage="RegularExpressionValidator" ValidationExpression="\d+"></asp:RegularExpressionValidator>


Use it
 
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