C# .NET - Mobile number textbox validation - Asked By vandana sardana on 06-Oct-09 03:43 AM

I have made the keypress event  only to take the numbers. Now i want that the digits entered should be 10 only not more than 10. if user enters more than 10 digits it will give error.

re - Web Star replied to vandana sardana on 06-Oct-09 03:53 AM

u can validate mobile no as follows using RegularExpressionValidator

<td>
                <asp:TextBox id="TextBox1"
                     runat="server"/>
             </td>
             <td>
                <asp:RegularExpressionValidator id="RegularExpressionValidator1"
                     ControlToValidate="TextBox1"
                     ValidationExpression="\d{10}"
                     Display="Static"
                     EnableClientScript="false"
                     ErrorMessage="Mobile noumer must be 10 numeric digits"
                     runat="server"/>
             </td>
          </tr>
          <tr>
             <td></td>
             <td>
                <asp:Button ID="Button1" text="Validate"
                     OnClick="ValidateBtn_Click"
                     runat="server"/>
             </td>

Mobile number textbox validation - mv ark replied to vandana sardana on 06-Oct-09 03:53 AM

Adapt this snippet -

string pattern = @"^\d{10}$";
        string data = "111111111111";
        if (Regex.IsMatch(data, pattern))
        {
            Console.WriteLine("Match");
        }
        else
        {
            Console.WriteLine("Not a Match");
        }

Include a reference to the namespace System.Text.RegularExpressions

use regular expression to validate the mobile number - Venkat K replied to vandana sardana on 06-Oct-09 03:56 AM

This below expressions will accept the country code as optional and the phone number as 10 digit number.

<script language="JavaScript">

function phone_validate(phone)
{

       var phoneReg = ^((\+)?(\d{2}[-]))?(\d{10}){1}?$;

       if(phoneReg.test(phone) == false)
       {

              alert("Phone number is not yet valid.");

       }
       else
       {

              alert("You have entered a valid phone number!");

       }

}

</script>

 
Thanks,
re - Web Star replied to vandana sardana on 06-Oct-09 04:02 AM

u can also achive this validation in javaSCRIPT simly as folllows

first put maxlength of textbox property to 10 for not exceed 10 digit and thhen use javascript function to validate it is numeric

1. u can validate in javascript by using this function

function isPhoneNumber(s)
{

     // Check for correct phone number
     rePhoneNumber = new RegExp(/^[1-9]\d{2}\s?\d{3}\d{4}$/);

     if (!rePhoneNumber.test(s)) {
          alert("Phone Number Must Be Entered ");
          return false;
     }

return true;
}

<asp:TextBox ID="TextBox1" MaxLength="10" runat="server"></asp:TextBox>

and call javascript function on button click event for validate mobile no


reply - Deepak Sonawane replied to vandana sardana on 06-Oct-09 04:05 AM
Hi,
I will suggest to use html input control instead of textbox server control:
Code for same:::
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <SCRIPT LANGUAGE="JavaScript">
// function parameters are: field - the string field, count - the field for remaining characters number and max - the maximum number of characters
function CountLeft(field, count, max) {
// if the length of the string in the input field is greater than the max value, trim it
if (field.value.length > max)
field.value = field.value.substring(0, max);
else
// calculate the remaining characters
{
count.value = max - field.value.length;
}
if(count.value=="0")
count.value ="Max 10";
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <input name="text" type="text" size="40"
onKeyDown="CountLeft(this.form.text,this.form.left,10);"
onKeyUp="CountLeft(this.form.text,this.form.left,10);">
<input readonly type="text" name="left" size=3 maxlength=3 value="10">
    </div>
    </form>
</body>
</html>

Mobile number textbox validation - DL M replied to vandana sardana on 06-Oct-09 05:07 AM
try this


<asp:RegularExpressionValidator id="RegularExpressionValidator4" runat="server" ControlToValidate="txtPhoneNumber" ErrorMessage="Please enter a default 10 digit contact number (eg:111-111-1111)." ValidationGroup="next3" ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}">*</asp:RegularExpressionValidator>

OR

dont forget to mark "Do you like answer" please :)

 private void btnValidatePhoneNumber_Click(object sender, EventArgs e)

        {

            Regex re = new Regex("^9[0-9]{9}");

 

            if(re.IsMatch(txtPhone.Text.Trim()) == false || txtPhone.Text.Length>10)

            {

                MessageBox.Show("Invalid Indian Mobile Number !!");

                txtPhone.Focus();

            }

 

        }


vishal replied to DL M on 06-Aug-10 01:51 AM

 private void btnValidatePhoneNumber_Click(object sender, EventArgs e)

        {

            Regex re = new Regex("^9[0-9]{9}");

 

            if(re.IsMatch(txtPhone.Text.Trim()) == false || txtPhone.Text.Length>10)

            {

                MessageBox.Show("Invalid Indian Mobile Number !!");

                txtPhone.Focus();

            }

 

        }

Regex re = new Regex("^9[0-9]{9}");
what is this