Click here to Skip to main content
16,023,339 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have to make regular expression which not allow string start with sprecial caracter.
for exampel :
45@gf.com - right
@45gm.com - wrong

how can i do this ?

What I have tried:

i have make this regular expression...
string TrNo = txtConsignor.Text.ToString();
TrNo = System.Text.RegularExpressions.Regex.Replace(TrNo, "[^a-zA-Z0-9\\s]", "");
txtConsignor.Text = TrNo.TrimStart();
Posted
Updated 12-May-17 1:47am
Comments
Patrice T 12-May-17 7:49am    
You say something and your code does something else.
What do you want to do with your RegEx?
you want to validate a string or change the string ?

1 solution

What you are doing doesn't prohibit special characters, it removes them. And it doesn't require them to be at the start, either!
So if your string is "@45gm.com" then it converts it to 45gmcom and doesn't tell anyone it's done it. So what gets stored (or otherwise processed) isn;t a valid email either, and gives problems way on down the line, when it's too late to fix it. Worse, it does the same thing with your "correct" string: "45@gf.com" becomes "45gfcom"

What you need is to use it as a regex and check the result:
C#
Match m = Regex.Match(trNo, "^[^a-zA-Z0-9\\s]");
if (m.Success)
    {
    ... report problem to user ...
    return;
    }
 
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