Click here to Skip to main content
15,888,151 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to validate name in C#

Example.
Consider name which has a Space, Full Stop(dot) and only words

Ravi Kumar.P

How to validate the above name pattern.
Posted
Updated 14-Feb-17 17:38pm
Comments
Kornfeld Eliyahu Peter 11-Mar-14 6:49am    
Read about regex - it can do magic...
KUMAR619 11-Mar-14 6:52am    
Can you help me for the above name
Kornfeld Eliyahu Peter 11-Mar-14 7:06am    
I can. However, doing so means that I write your code. That's something that not fit to me...
I prefer that you do some reading (use Google) about regex in c#, than try to write some code. AS your request is simple it's possible that you will come up with something working at the very first time, but if not you can come back with that code so someone show you how to fix it...
SwarupDChavan 11-Mar-14 6:58am    
do you need to do the validation on the client-side??
KUMAR619 11-Mar-14 6:59am    
yes

Try this:
@"^[A-Za-z]+[\s][A-Za-z]+[.][A-Za-z]+$"

Read more: regex-match[^]
 
Share this answer
 
v3
Comments
KUMAR619 11-Mar-14 7:07am    
Thanks if name has N number of spaces

such

Ram Ram

Kumar Kumar Kumar
Ravi Ravi

Ram

Sometimes without space


Then what is the common match string to achieve this type of problem
Peter Leow 11-Mar-14 7:28am    
Try this: @"^[A-Za-z\s]+[.][A-Za-z\s]+$"
A very good tutorial here: http://www.codeproject.com/Articles/9099/The-Minute-Regex-Tutorial
Learn to DIY yourself.
Kornfeld Eliyahu Peter 11-Mar-14 7:08am    
[A-Za-z] is not exactly word in regex...\w is defined as [A-Za-z0-9_].
In his case I think \b should be even better...
Try Regex:
C#
public static Regex regex = new Regex(
      "^(\\b[A-Za-z]*\\b\\s+\\b[A-Za-z]*\\b+\\.[A-Za-z])$",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );

...
   if (regex.IsMatch(InputText))
      {
      // All ok
      }



"Is there any way to get string for the above names"


Try:
C#
public static Regex regex = new Regex(
      "^(\\b[A-Za-z]*\\b(\\s+\\b[A-Za-z]*\\b)*(\\.[A-Za-z])?)$",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );
 
Share this answer
 
v2
Comments
KUMAR619 11-Mar-14 7:14am    
User name is not static sometimes it may have blank space and sometimes it may not

Consider following names

Ram

Ram Kumar

Raja Ram Mohan

Is there any common string for all the above names
KUMAR619 11-Mar-14 7:20am    
Is there any way to get string for the above names
OriginalGriff 11-Mar-14 7:38am    
Answer updated
KUMAR619 11-Mar-14 8:42am    
Thanks you are deserved to get 10 stars for this solution
C#
function ValidateData(){

      var TextVal= document.getElementById('<%=txtbox1.ClientID %>').value;
          if (TextVal== "") {
              alert("text box Can't be empty ");
              document.getElementById('<%=txtbox1.ClientID%>').focus();
              return false;
          }
          
          var textpart = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
          var validtext = textpart .test(TextVal);
         
          if (!validtext ) {
              alert("Your textbox contain invalid ..... again.");
              document.getElementById('<%=txtEmailID.ClientID%>').focus();
              return false;}
}
else
return true



call ValidateData() in clientclik event ......
 
Share this answer
 
v2
Use Regular Expression .
Regex.IsMatch(string,string) method can be used which take your name string and regular expression string as parameters.
 
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