ASP.NET - How to restrict user - Asked By bhanupratap singh on 07-May-14 08:49 AM

Hi, Morrie,
I need to prevent user that user should supply only "P" or "p" alpsha in textbox. if user supply like P1235P or any toher alpha rathar than "P" or "p" , user should not be able to go forward.
I need value in text box Like  P1234565454. Single P character is allowed rest alpha character are not allowed. all numbers are allowed
thanks

Robbe Morris replied to bhanupratap singh on 07-May-14 10:49 AM
var test = this.txtMyTextBox.Text.ToUpper();
var testOfNumbers = test.Replace("P","");

if (!test.StartsWith("P"))
{
   MessageBox("You are a doofus.  It must start with a P");
}

if (ToInt(testOfNumbers ) <1) 
{
    MessageBox("invalid entry");
}

 // Safely converts all sorts of text entries to a number if the originalValue only contains a number.  Returns 0
// if the text contains something other than a number.  Does not raise an exception.
 public Int32 ToInt(string originalValue)
 {
   Int32 returnValue = 0;
   if (String.IsNullOrEmpty(originalValue)) return 0;
   originalValue = originalValue.Trim();
   if (originalValue.Length < 1) return 0;
   int.TryParse(originalValue, out returnValue);
   return returnValue;
 }
bhanupratap singh replied to Robbe Morris on 08-May-14 12:59 AM
Thanks Morri.
it's great