1. In our daily programming as we play with form we always need a validator which can validate our text like "Names" , "School", "Organization".
So we need to sanitize this type of input, so that it can't accept (‘@’, ‘-‘ and ‘.’:) values.
For this i have got an regex which i have posted below:
private static string removeUnwantedCharacters(String inputvalue)
{
return Regex.Replace(inputvalue, @"[^\w\.@-]", string.Empty);
}
we can test this regex as:
string getResult = removeUnwantedCharacters("()hi{ev??er#'y>>one<<");
…returns “hieveryone”.
2. Many times we also have to face problem with Date correct formats.
Here's a regex which can be changed as per your format needs.
private static string FormatMyDate(String dateInputvalue)
{
return Regex.Replace(dateInputvalue, "\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b" , "${day}-${month}-${year}");
}
Or In Clear way
using System.Text.RegularExpressions;
string datepattern = @"(?<mm>\d{2}).(?<dd>\d{2}).(?<yyyy>\d{4})";
string replaceStr = @"${yyyy}-${mm}-${dd} or ${dd}.${mm}.${yyyy}";
Regex myRegEx = new Regex(datepattern);
string datevalue = Request.Params["GetDateFromControlName"].ToString()
string myNewDateWithPattern = datevalue.ReplaceAll(myRegEx, replaceStr);
and further we can convert our date to datetime datatype.
this will act as same as image given below we can use this to replace all with correct format too
0 comments:
Post a Comment