如何强制输入的 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 来检查范围并使用货币 RegEx。谢谢迈克尔·尼斯克恩。
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" />