Click here to Skip to main content
16,010,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text box which should accept the userid in such a way that it should start with an "u" and then 7 numbers.

can any one help me out please.
Posted
Updated 4-Sep-13 21:35pm
v2

Well You can use RegularExpressionValidator which is a control of ASp along with combination of Regex....

Like below.


XML
<asp:RegularExpressionValidator ID="validator1" runat="server" BackColor="Red" ControlToValidate="ControlID" ValidationExpression="[u]{1}[0-9]{7}" ErrorMessage="ValidationMessageHere" >
  </asp:RegularExpressionValidator>



[u]{1}[0-9]{7} will allow a string starting with 'u' followed by 7 digits...

if you want to allow user to enter numbers counting between 1 and 7 dn you can use the following..>

[u]{1}[0-9]{0,7}

it will allow

u12
u123
u1234
..... u1234567

Hope this can solve your query... :)
 
Share this answer
 
Comments
VICK 5-Sep-13 3:57am    
Thanks for Accepting... :)
XML
<asp:TextBox ID="txt_userid" runat="server" />
    <asp:CustomValidator ID="CustomValidator" runat="server"     EnableClientScript="true"
        ErrorMessage="Userid should start with letter u followed with 7 digits"
        ClientValidationFunction="validateuserid"
        ControlToValidate="txt_userid" Display="Dynamic" >
    </asp:CustomValidator>




<script type="text/javascript">

function validateuserid(sender, args) {
var Userid = document.getElementById('txt_userid').value;

if (Userid.length == 8) {
if (Userid.substring(0, 1) == 'u') {
args.IsValid = true;
}
else {
var regex = '^[0-9]{7}$';
var str = Userid.substring(1, 8)
if (str.search(regex) == -1) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
}
else {
args.IsValid = false;
}
}
 
Share this answer
 
Hi Abhijit, try this;


ASP.NET
<asp:customvalidator id="CustomValidator1" controltovalidate="<b" xmlns:asp="#unknown">"Your ControlID" OnServerValidate="cusCustom_ServerValidate" ErrorMessage="Your Msg!" runat="server"></asp:customvalidator>


use this functions;

C#
protected void cusCustom_ServerValidate(object sender, ServerValidateEventArgs e)
   {
       if (e.Value.Length == 8)
       {
           if (e.Value[0] == 'U')
           {
               System.Globalization.NumberStyles ns = System.Globalization.NumberStyles.Integer;

               string str = e.Value.Substring(1, e.Value.Length - 1).ToString();
               bool bol = isNumeric(str, ns);
               if (bol == true)
               {
                   e.IsValid = true;
               }
               else
               {
                   e.IsValid = false;
               }
           }
           else
           {
               e.IsValid = false;
           }
       }
       else
       {
           e.IsValid = false;
       }
   }

   public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
   {
       Double result;
       return Double.TryParse(val, NumberStyle,
           System.Globalization.CultureInfo.CurrentCulture, out result);
   }



I hope this will resolve your problem.

Thank's
Mohan G
 
Share this answer
 
v2
if(document.getElementById('<%= textbox.ClientID%>').value=="")

{
alert("Please Enter Number");
document.getElementById('<%= textbox.ClientID%>').focus();
return false;
}

var y = document.getElementById("<%=textbox.ClientID %>").value;

if(document.getElementById('<%= txtmobileno.ClientID %>').value != "")
{
if (y.length > 7 || y.length < 7)
{
alert("Please Enter 7 characters");
document.getElementById("<%=textbox.ClientID %>").focus();
return false;
}


Assign U as Static in TextBox
 
Share this answer
 
v2

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