Thursday, March 22, 2012

Textbox

Dear The Expert,
I need to set the textbox to allow numeric input only and it automatically display with thousand separator and 2 decimal symbol.. how to do this?
Please help
Thanks
WinanjayaThere are a few ways to do it. there are validator controls that will check tha value of your text box client side or you can validate on the server side using regular expressions.

public static bool Validate(string DirtyString, string RegularExpresion)
{
bool passed = true;
System.Web.HttpContext curr = System.Web.HttpContext.Current;
string currcell = curr.Server.HtmlEncode(DirtyString);
currcell = currcell.Trim();
if (passed == PassedFilter(currcell,RegularExpresion))
{
return true;
}
else
{
return false;
}
}

public static bool PassedFilter(string DirtyString, string FilterExp)
{
string cleanstring = "";

foreach (Match charMatch in System.Text.RegularExpressions.Regex.Matches(DirtyString, FilterExp))
{
cleanstring = cleanstring + charMatch;
}
if (cleanstring.Length == DirtyString.Length)
{
return true;
}
else
{
return false;
}
}


This is the server side way. Pass the string you want to validate and the regular expression to validate against it.

0 comments:

Post a Comment