How to limit which characters are permitted in strings? - c#

I'm currently making a user class in c# that contains a first name, last name, username and email.
The username can only contain numbers [0-9], lower-case letters [a-z] and underscores '_'
The email can only contain [a-z], [A-Z], [0-9], as well as dot '.', comma ',', underscore '_' and hyphen '-'
How are these limitations set to strings in c#?

You could do this logic in the setters of the properties.
ex:
private string _firstName;
public string FirstName
{
get { return this._firstName; }
set
{
if (Regex.Match(value, YOUR_REGEX).Success)
this._firstName = value;
}
}

Sounds like the goal of this project is to get you to know how to use Regular Expressions. A good online resource for this which contains common patterns and testing is located at the Regular Expression Library
The simple email validation requirement you have can also be done with RegEx, but is actually incorrect as it will allow invalid addresses through; commas can be in an email address but they would need to be quoted which your pattern does not allow. The specifications for the local portion of an email address is probably one of the most poorly implemented standards on the internet.
For future reference, a cheat for checking email addresses within .Net is to use the MailAddress class of the System.Net.Mail namespace. You can use a try...catch routine to see if the submitted address can be converted. The only problems to be aware of using this is it will allow addresses through which most people would not consider to be valid even though they are; such as a server name without an extension (.com etc).
private bool isValidEmail (string emailAddress) {
bool ReturnValue;
try {
MailAddress ma = new MailAddress(emailAddress);
ReturnValue = true;
}
catch (Exception) { ReturnValue = false; }
return ReturnValue;
}

Related

How to write a data annotation to check if email address found in string and fail on it

I have the following code but it's not quite working... Any ideas where i'm going wrong? It seems to be failing when there is a carriage return in the string? (see fiddles at bottom)
[RegularExpression(#"^((?!([\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*#((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3})))).)*$", ErrorMessage = "Please do not include an email address in your description.")]
Context
People are writing descriptions and also placing email addresses in them, for example:
"Hi there i'm bob, here is my email: example#exampele.com. Hope you
havea great day at my party."
I need to check that string and see that there is an email, and then not allow it to be submitted. (I'm using Entity framework and data annotations alongside the jquery validation in ASP .NET MVC 5... This is why i mentioned Data annotation usage.
Note:
I took the inversion technique from here:
Jquery validation on matching 'password' and 'admin' not working
And the email validation from here:
Best Regular Expression for Email Validation in C#
Attempts:
The following string:
http://pastebin.com/00BE7tUW
will show the error, whereas this will not:
http://pastebin.com/i69uxzRf
So something is a little wrong in the expression considering there is no email in it?
Fiddle:
Not working: https://regex101.com/r/zL7xD7/1
Working: https://regex101.com/r/hJ8fJ9/1
Working with email: https://regex101.com/r/dB3cU2/1
Have you tried something like this:
bool invalid = false;
public bool IsValidEmail(string strIn)
{
invalid = false;
if (String.IsNullOrEmpty(strIn))
return false;
// Use IdnMapping class to convert Unicode domain names.
try {
strIn = Regex.Replace(strIn, #"(#)(.+)$", this.DomainMapper,
RegexOptions.None, TimeSpan.FromMilliseconds(200));
}
catch (RegexMatchTimeoutException) {
return false;
}
if (invalid)
return false;
// Return true if strIn is in valid e-mail format.
try {
return Regex.IsMatch(strIn,
#"^(?("")("".+?(?<!\\)""#)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])#))" +
#"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
}
catch (RegexMatchTimeoutException) {
return false;
}
}
private string DomainMapper(Match match)
{
// IdnMapping class with default property values.
IdnMapping idn = new IdnMapping();
string domainName = match.Groups[2].Value;
try {
domainName = idn.GetAscii(domainName);
}
catch (ArgumentException) {
invalid = true;
}
return match.Groups[1].Value + domainName;
}
The IsValidEmail method returns true if the string contains a valid email address and false if it does not, but takes no other action.
To verify that the email address is valid, the IsValidEmail method calls the Regex.Replace(String, String, MatchEvaluator) method with the (#)(.+)$ regular expression pattern to separate the domain name from the email address. The third parameter is a MatchEvaluator delegate that represents the method that processes and replaces the matched text.
Then you would check the email address by doing something like:
IsValidEmail(emailAddress)
Does this work for you?
The . needed to be replaced by [\S\s]
[RegularExpression(#"^((?!([\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*#((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3}))))[\S\s])*$", ErrorMessage = "Please do not include an email address in your field description.")]
source: Regular expression is not matching new lines

Regular Expression for allowing multiple language input

Quick question regarding regular expression validation on textbox entry. Basically I have a textbox that I am using for user input in the form of a website address. The user can input anything (it doesn't have to be a valid website address - i.e. www.facebook.com. They could enter "blah blah", and that's fine but it will not run.
What I am after is to validate different languages, Arabic, Greek, Chinese, etc etc, because at present I only allow English characters.
The code for the method is below. I believe I will have to switch this from a whitelist to blacklist, so instead of seeing what matches, change the expression to invalid characters, and if the user enters one of these, don't allow it.
public static bool IsValidAddress(string path)
{
bool valid = false;
valid = (path.Length > 0);
if (valid)
{
string regexPattern = #"([0-9a-zA-Z*?]{1})([-0-9a-zA-Z_\.*?]{0,254})";
// Elimate the '"' character first up so it simplifies regular expressions.
valid = (path.Contains("\"") == false);
if (valid)
{
valid = IsValidAddress(path, regexPattern);
}
if (valid)
{
// Need an additional check to determine that the address does not begin with xn--,
// which is not permitted by the Internationalized Domain Name standard.
valid = (path.IndexOf("xn--") != 0);
}
}
return valid;
}
As you can see, I have the 0-9a-zA-Z included, but by default this will eliminate other languages, whereas I wish to include the languages.
Any help is greatly appreciated. If I've confused anyone, sorry! I can give more information if it is needed.
Thanks.
I don't know why you're trying to validate Uri's with Regex. .Net's Uri class is surely a much better match to your task, no?
Uri uri;
if(!Uri.TryParse(uriString, UriKind.Absolute, out uri))
{
//it's a bad URI
}

Code an elegant way to strip strings

I am using C# and in one of the places i got list of all peoples names with their email id's in the format
name(email)\n
i just came with this sub string stuff just off my head. I am looking for more elegant, fast ( in the terms of access time, operations it performs), easy to remember line of code to do this.
string pattern = "jackal(jackal#gmail.com)";
string email = pattern.SubString(pattern.indexOf("("),pattern.LastIndexOf(")") - pattern.indexOf("("));
//extra
string email = pattern.Split('(',')')[1];
I think doing the above would do sequential access to each character until it finds the index of the character. Works ok now since name is short, but would struggle when having a large name ( hope people don't have one)
A dirty hack would be to let microsoft do it for you.
try
{
new MailAddress(input);
//valid
}
catch (Exception ex)
{
// invalid
}
I hope they would do a better job than a custom reg-ex.
Maintaining a custom reg-ex that takes care of everything might involve some effort.
Refer: MailAddress
Your format is actually very close to some supported formats.
Text within () are treated as comments, but if you replace ( with < and ) with > and get a supported format.
The second parameter in Substring() is the length of the string to take, not the ending index.
Your code should read:
string pattern = "jackal(jackal#gmail.com)";
int start = pattern.IndexOf("(") + 1;
int end = pattern.LastIndexOf(")");
string email = pattern.Substring(start, end - start);
Alternatively, have a look at Regular Expression to find a string included between two characters while EXCLUDING the delimiters

regular expression for indian phone number

am looking for the regular expression of indian phone number
the regular expression should allow all the following formats.
for landline no
0802404408
080-2404408
+91802404408
+91-802404408
for mobile no
8147708287
08147708287
+918147708287
+91-8147708287
can anyone help me, thanks in advance
my code is
[RegexValidator("[0-9 -]*"
, MessageTemplateResourceName = "INVALID_PHONE"
, MessageTemplateResourceType = typeof(ValidatioinErrors))]
public string Phone
{
get { return phone; }
set { phone = value; }
}
public bool IsValid()
{
return Validation.Validate<Class_name>(this).IsValid;
}
public ValidationResults ValResults
{
get
{
return Validation.Validate<Class_name>(this);
}
}
for this validation thing I just referred
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
in my namespace, in the UI part the expression is working fine, but in the code behind as above, it shows "Invalid Phone number", if I give value as 080-2404408
You can try
^\+?[0-9-]+$
See it here on Regexr
The important parts are the anchors ^ and $ for the start and the end of the string. I added also \+? at the start, to match an optional +. The + needs to be escaped since it is a special character in regex, the ? after it makes it optional.
Of course this is a very simple pattern, be aware that e.g. "-----" would also be valid.
For the examples provided following RegEx works:
/^(?:\+91(?:-)?|0\d{2,4}-|0|)\d{7,10}$/
import re
mystr = """
8147708287
08147708287
+918147708287
+91-8147708287
"""
print(re.findall(r'\b91-*\d{10}',mystr))

Url mapping in web forms - allow question marks and hyphens

I have implemented URL mapping in our ASP.NET 4 application, but I have a problem with some of our content.
Some of our products has a hyphen "-" or a question mark "?" in them. It's not an option to remove that. A productname could be "My Product - Good for you?".
We use two custom made methods, MakeUrlSeoReady and MakeUrlNonSeoReady. We replace space like this: Replace(" ","-"), as this is the most SEO-friendly solution. However, we also need to make this work with both question marks and hyphens.
The reason we use the MakeUrlSeoReady / NonReady methods is to be able to show the "real" name.
Currently the mapping is defined as follows:
routes.MapPageRoute("Produkt visning",
"artikler/{Categoryname}/{SubCategoryname}/{ProductName}",
"~/SingleProduct.aspx");
So what I do is I retrieve the product depending on the ProductName. I use two methods I've created:
public static string MakeUrlNonSeoReady(string text)
{
return text.ToLower().
Replace("oe", "ø").
Replace("aa", "å").
Replace("ae", "æ").
Replace("-", " ");
}
public static string MakeUrlSeoReady(string text)
{
return text.ToLower().
Replace("ø", "oe").
Replace("å", "aa").
Replace("æ", "ae").
Replace(" ", "-");
}
So in the SingleProduct.aspx page I use the following string to get from our database:
string categoryName = HelperFunctions.MakeUrlNonSeoReady(Page.RouteData.Values["ProductName"]);
But this will of course not work. So any help is really appreciated :-)
An arguably cleaner and simpler method is to use a unique product identifier that is numerical or alphanumerical and is natively HTML encoded, and then simply put the product name as an unused parameter for SEO or search purposes.
MSDN RouteCollection.MapPageRoute Method (String, String, String, Boolean, RouteValueDictionary)
routes.MapPageRoute("Produkt visning",
"artikler/{Categoryname}/{SubCategoryname}/{ProductIdentifier}/{ProductName}",
"~/SingleProduct.aspx", false, new RouteValueDictionary
{ { "ProductName ", string.Empty } });

Categories

Resources