我正在写一个html表单。在我的提交按钮的代码中,我使用“onclick”引用了两个Javascript函数,不知怎的,我没有按照预期从其中任何一个接收警报消息。如果任何表单字段为空,函数“notEmpty”显示警告消息,函数“VerifyPhoneNumber”将通知用户输入的电话号码是否不符合以下格式: XXX-XXX-XXXX。 (X表示数字)
代码如下:
<html>
<head>
<script type='text/javascript'>
function notEmpty(elementone, elementtwo, elementthree, helperMsg){
if(elementone.value.length == 0 || elementtwo.value.length == 0 || elementthree.value.length == 0){
alert(helperMsg);
elem.focus();
return false;
}
return true;
}
function VerifyPhoneNumber(number, errormessage){
for(int i=0; i < number.toString().length; i++){
var space = number.substr(i,1);
var type = isNaN(space);
if (type != false){
if (space != '-' && i != 4 && i != 8){
alert(errormessage);
elem.focus();
return false;
}
}
}
return true;
}
</script>
</head>
<body>
<p>Please input the following information:</p>
<form method="post" onSubmit="return formValidator()">
Name:
<input type="text" id="name">
Address:
<input type="text" id="address">
Phone Number:
<input type="text" id="phonenumber">
<input type="submit" onclick="notEmpty(document.getElementById('name'), document.getElementById('address'), document.getElementById('phonenumber'), 'one or more of the question fields seems to be empty'); VerifyPhoneNumber(document.getElementById('phonenumber'), 'that is not a complete phonenumber');">
</form>
</body>
</html>