 |
|
 |
Hi!,
thanks for the help!.
... where is the FilterNumeric() function source code?
|
|
|
|
 |
|
 |
i would like to limit the length of a multi line textbox
how can i define such function which will limit length of a multi line textbox(the length should not exceed more than 500 words)
|
|
|
|
 |
|
 |
anyway, thanks for sharing your code!
|
|
|
|
 |
|
 |
can u guide us how to make masking on these controls?
like date/currency/phone numbers
|
|
|
|
 |
|
 |
I'm a complete ASP.NET nOOb, so while your solution looks complete and elegant, after peering into the Zip file I downloaded, I have *NO* idea where to start! ANy chance of getting a step-by-step set of instructions on how to include this in my project (and also on how to deploy it to clients)?
Thanks much!
Joel Leitner
|
|
|
|
 |
|
 |
This code doesn't work in firefox. On Internet Explorer perfectly though. Are you posting an update for this?
|
|
|
|
 |
|
 |
Okay, I've got it. This code should do the trick. Also solves the Globalisation error.
Could someone check this for other browsers? I've only checked it for IE en FF.
///
/// Returns script used to validate numbers
///
///
protected string GetNumberValidatorScript()
{
string _str = @"
<script language=""javascript""><!--
function FilterNumeric
{
evt = (evt) ? evt : ((window.event) ? event : null);
if (evt)
{
var charCode = (evt.charCode) ? evt.charCode :
((evt.keyCode) ? evt.keyCode :
((evt.which) ? evt.which : 0));
var ch = String.fromCharCode(charCode);
if (charCode < 32)
{
return true;
}
if((charCode <= 57) && (charCode >= 48))
{
if (!evt.shiftKey)
{
return;
}
}
if ((ch=='" +
NumberFormatInfo.CurrentInfo.NegativeSign +
"') || (ch=='" +
NumberFormatInfo.CurrentInfo.NumberDecimalSeparator + @"'))
{
return;
}
if (window.event) evt.returnValue = false;
else evt.preventDefault();
return false;
}
}
--></script>";
}
and don't forget to set the this.txtNumber.Attributes.Add ("onkeypress", "FilterNumeric()"); to this.txtNumber.Attributes.Add ("onkeypress", "FilterNumeric(event)");
|
|
|
|
 |
|
 |
Little update:
function FilterNumeric should ofcourse be function FilterNumeric(evt)
|
|
|
|
 |
|
|
 |
|
 |
No problem. Your example was very useful so I used your code in my project. While doing that I found some things that could be improved and simplified and those improvements should of course be brought back to the Code Project community.
Thanks for your article.
|
|
|
|
 |
|
 |
It's not my article I'm thinking about writing my own seeing how there is no simple, free and good ASP.NET numeric control.
|
|
|
|
 |
|
 |
Oeps.. I wasn't paying attention. Actualy i was also thinking of posting an article based on the code on this article with my changes.
Let's look who's first
|
|
|
|
 |
|
|
 |
|
 |
Me.Attributes.Add("onkeypress", "FilterNumeric(event)")
should be
Me.Attributes.Add("onkeypress", "return FilterNumeric(window.event)")
|
|
|
|
 |
|
 |
Can you explain why that is? When is this going wrong?
|
|
|
|
 |
|
 |
I looked at your code and saw that you called the "CurrentInfo.NumberDecimalSeparator" while building the RegularExpressionValidator, but not while building the FilterNumeric function.
I haven't been testing it, but I suppose that "if ((ch=='-') ||(ch=='.'))" still causes problems in other Languages.
And I'm wondering why you're using your own validator instead of the CompareValidator? I suppose the code below should do the trick also:
protected CompareValidator BuildNumberValidator(string iIDArg,string strErrArg)
{
CompareValidator cv = new CompareValidator();
cv.ControlToValidate = iIDArg;
cv.Operator = ValidationCompareOperator.Equal;
cv.Type = ValidationDataType.Double;
return cv;
}
|
|
|
|
 |
|
 |
I'm sorry about that. The code in the previous post is actually wrong. What it should have been was this:
protected CompareValidator BuildNumberValidator(string iIDArg,string strErrArg)
{
CompareValidator cv = new CompareValidator();
cv.ControlToValidate = iIDArg;
cv.Operator = ValidationCompareOperator.DataTypeCheck;
cv.Type = ValidationDataType.Double;
return cv;
}
In other words, the ValidationCompareOperator should have been DataTypeCheck and not Equal.
Note that the Validators in ASP.NET work well with the users culture, so some difficult RegExp are not necessarily.
Some other improvements could be made to the control.
1. Try using a StringBuilder (or even better JavaScriptBuilder) for the GetNumberValidatorScript() function. += operations are quite expensive in C#.
2. Create a NumericTextBox control that derives from TextBox, implements the GetNumberValidatorScript() function and calls the Page.RegisterClientScriptBlock() and this.Attributes.Add() in the OnInit() event. This way you have a more reusable solution.
|
|
|
|
 |