我有一个方法可以根据 3 个正则表达式验证字段并根据失败的表达式返回错误。
function mfpValidateValue()
{
var pCtrl = window.document.forms[0].txtValue;
var pStrValue = mTrim(pCtrl.value);
if (pStrValue == '')
return true;
var regexNum = new RegExp("^[0-9]{9}.{0,3}$"); // First 9 are numeric followed by up to any 3 characters
var regexLetter1 = new RegExp("^[A-Z]{1,3}[0-9]{6}$"); //Up to the first 3 are alpha, then there are exactly 6 numbers
var regexLetter2 = new RegExp("^[A-Z]{1,3}[0-9]{9}$"); //Up to the first 3 are alpha, then there are exactly 9 numbers
var error = "";
// If any of the RegEx fails, set base error message
if (!regexNum.test(pStrValue) || !regexLetter1.test(pStrValue) || !regexLetter2.test(pStrValue))
error = "Please enter a valid Value.";
// Set more specific error message.
if (!isNaN(pStrValue.charat(0)))
error += " If the first character of Value is a digit, then the first nine characters must be digits.";
else
error += " If the first character of Value is a letter, up to the first three characters must be letters proceeded by 6 or 9 digits.";
return (error == "");
}
我在这一行收到以下错误消息:
if (!isNaN(pStrValue.charat(0)))
Object doesn't support property or method 'charat'
pStrValue
中的值为:
"12345678"
JavaScript 是否在这里模糊地使用术语“对象”来指代我的特定变量,或者它实际上认为 pStrValue
是一个对象而不是字符串?
最佳答案
你犯了一个小错误。 charat()
不是函数,但 charAt()
是。
你的代码应该是
if (!isNaN(pStrValue.charAt(0)))
这是函数
关于javascript - 检查字符串的第一个字符是否为数字会报错,表明 charat 不是有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32103038/