删除“$” 在 ASP.Net RangeValidator 脚本执行之前
如何强制输入的 onchange 脚本在 RangeValidator 的脚本之前运行?
我想防止用户输入美元符号或逗号时验证失败。
function cleanUp(str) {
re = /^\$|,/g;
return str.replace(re, ""); // remove "$" and ","
}
<input type="text" id="salary" runat="server"
onchange="this.value=cleanUp(this.value)" />
<asp:RangeValidator ID="salaryValidator"
runat="server" ErrorMessage="Invalid Number"
ControlToValidate="salary" Type="Double" />
更新:
我决定使用 CustomValidator 来检查范围并使用货币正则表达式。 谢谢迈克尔·尼克斯肯。
function IsCurrency(sender, args) {
var input = args.Value;
// Check for currency formatting.
// Expression is from http://regexlib.com/REDetails.aspx?regexp_id=70
re = /^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$/;
isCurrency = input.match(re);
if (isCurrency) {
// Convert the string to a number.
var number = parseFloat(CleanUp(input));
if (number != NaN) {
// Check the range.
var min = 0;
var max = 1000000;
if (min <= number && max >= number) {
// Input is valid.
args.IsValid = true;
return;
}
}
}
// Input is not valid if we reach this point.
args.IsValid = false;
return;
}
function CleanUp(number) {
re = /^\$|,/g;
return number.replace(re, ""); // remove "$" and ","
}
<input type="text" id="salary" runat="server" />
<asp:CustomValidator ID="saleryValidator" ControlToValidate="salary" runat="server"
ErrorMessage="Invalid Number" ClientValidationFunction="IsCurrency" />
How can I force the input's onchange script to run before the RangeValidator's script?
I want to prevent a failed validation when the user enters a dollar sign or comma.
function cleanUp(str) {
re = /^\$|,/g;
return str.replace(re, ""); // remove "$" and ","
}
<input type="text" id="salary" runat="server"
onchange="this.value=cleanUp(this.value)" />
<asp:RangeValidator ID="salaryValidator"
runat="server" ErrorMessage="Invalid Number"
ControlToValidate="salary" Type="Double" />
UPDATE:
I decided to use a CustomValidator that checks the range and uses a currency RegEx. Thanks Michael Kniskern.
function IsCurrency(sender, args) {
var input = args.Value;
// Check for currency formatting.
// Expression is from http://regexlib.com/REDetails.aspx?regexp_id=70
re = /^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$/;
isCurrency = input.match(re);
if (isCurrency) {
// Convert the string to a number.
var number = parseFloat(CleanUp(input));
if (number != NaN) {
// Check the range.
var min = 0;
var max = 1000000;
if (min <= number && max >= number) {
// Input is valid.
args.IsValid = true;
return;
}
}
}
// Input is not valid if we reach this point.
args.IsValid = false;
return;
}
function CleanUp(number) {
re = /^\$|,/g;
return number.replace(re, ""); // remove "$" and ","
}
<input type="text" id="salary" runat="server" />
<asp:CustomValidator ID="saleryValidator" ControlToValidate="salary" runat="server"
ErrorMessage="Invalid Number" ClientValidationFunction="IsCurrency" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是否尝试过使用 CustomerValidator 控件并将 JS 清理方法和 RangeValidator 方法的功能结合起来。
Have you tried using a CustomerValidator control and combined the functionality of the JS cleanup methods and the RangeValidator method.
我想我可以改进这一点。 这使得逗号和分位数字可选:
I think I can improve on that. This makes commas and cents digits optional:
有一种方法可以通过注册脚本来做到这一点; 但是为什么不使用正则表达式验证器来确保输入正确呢?
此外,范围验证器在字段 onBlur js 事件上执行,而不是在更改时执行。
There is a way to do this by registering the script; however why not use a Regular Expression Validator to make sure the input is proper?
Also, the Range validator executes on the fields onBlur js event, not on change.
刚刚注意到你有一个“.” 对于小数点,但这意味着正则表达式将接受该位置的任何字符。 您应该使用
\.
作为该小数点。Just noticed that you have a '.' for the decimal point, but that means the regex will accept any character in that spot. You should use
\.
for that decimal point.