我是JavaScript的新手并想要自己学习我有一个问题,我的警告框没有出现在Verify()方法的第二种方法是我做错了什么或别的什么!请帮助我提前谢谢!我的代码:
<html>
<head>
<script>
function Verify(){
if(!isValidName()){
return false;
}
if(!isValidID()){
return false;
}
}
function isValidName(){
var fnam=document.getElementById('fname').value;
var lnam=document.getElementById('lname').value;
var fcn=fnam.charAt(0);
var lcn=lnam.charAt(0);
if(fnam==""||fnam=="First Name"){
alert("Please Enter First Name!");
return false;
}
else if(lnam==""||lnam=="Last Name"){
alert("Please Enter Last Name!");
return false;
}
else if(fcn!=fcn.toUpperCase()){
alert("Please Use Uppercase for the First Letter in the First Name!");
return false;
}
else if(lcn!=lcn.toUpperCase()){
alert("Please Use Uppercase for the First Letter in the Last Name!");
return false
}
else{
var fc=0;
for(i=0; i<fnam.length; i++){
fc+=1;
if(fnam.charAt(i)>=0||fnam.charAt(i)<=9){
alert("Your First Name Contains an Integer! at index="+fc);
return false;
break;
}
}
var lc=0;
for(j=0; j<lnam.length; j++){
lc+=1;
if(lnam.charAt(j)>=0||lnam.charAt(j)<=9){
alert("Your Last Name Contains an Integer! at index="+lc);
return false;
break;
}
}
}
}
function isValidID(){
var g=document.getElementById('aaa').value;
if(g=="abc"){
alert("SOMETHING WRONG!");
return false;
}
}
function clearLastName(){
document.getElementById('lname').value="";
}
function clearFirstName(){
document.getElementById('fname').value="";
}
</script>
<title>
This is NIC Form Example
</title>
</head>
<body>
<h1>
This is NIC Form Example!
</h1>
<form name="nicform" onsubmit="return Verify()">
<table border="1">
<tr>
<td>
First Name:
</td>
<td>
<input type="text" id="fname" value="First Name" onClick="clearFirstName()">
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" id="lname" value="Last Name" onClick="clearLastName()">
</td>
</tr>
<tr>
<td>
Gender:
</td>
<td rowspan="2">
Male: <input type="radio" id="male" name="rad">
<br/>
Female: <input type="radio" id="female" name="rad">
</td>
</tr>
<tr>
</tr>
<tr>
<td>
<input type="text" id="aaa" value="abc" >
</td>
<td align="center" colspan="2">
<button type="submit">GO</button>
</td>
</tr>
</table>
</form>
</body>
</html>
问题是,如果带有id&#39; aaa&#39;的字段的值保持不变,即如果提交表格,其价值是&#34; abc&#34;它没有显示警报框!
答案 0 :(得分:2)
我认为永远不会调用第二种方法,因为如果每件事都是正确的,你会错过两个函数中的“return true”。 所以如果你补充说它会解决你的问题。
如果函数1执行正确,则必须要求返回true。如下所示
function Verify() {
if (!isValidName()) {
return false;
}
if (!isValidID()) {
return false;
}
}
function isValidName() {
var fnam = document.getElementById('fname').value;
var lnam = document.getElementById('lname').value;
var fcn = fnam.charAt(0);
var lcn = lnam.charAt(0);
if (fnam == "" || fnam == "First Name") {
alert("Please Enter First Name!");
return false;
}
else if (lnam == "" || lnam == "Last Name") {
alert("Please Enter Last Name!");
return false;
}
else if (fcn != fcn.toUpperCase()) {
alert("Please Use Uppercase for the First Letter in the First Name!");
return false;
}
else if (lcn != lcn.toUpperCase()) {
alert("Please Use Uppercase for the First Letter in the Last Name!");
return false
}
else {
var fc = 0;
for (i = 0; i < fnam.length; i++) {
fc += 1;
if (fnam.charAt(i) >= 0 || fnam.charAt(i) <= 9) {
alert("Your First Name Contains an Integer! at index=" + fc);
return false;
break;
}
}
var lc = 0;
for (j = 0; j < lnam.length; j++) {
lc += 1;
if (lnam.charAt(j) >= 0 || lnam.charAt(j) <= 9) {
alert("Your Last Name Contains an Integer! at index=" + lc);
return false;
break;
}
}
}
return true;
}
function isValidID() {
var g = document.getElementById('aaa').value;
if (g == "abc") {
alert("SOMETHING WRONG!");
return false;
}
return true;
}
function clearLastName() {
document.getElementById('lname').value = "";
}
function clearFirstName() {
document.getElementById('fname').value = "";
}
答案 1 :(得分:0)
如果表单有效,则函数Verify()
应返回true
。
function Verify(){
if(!isValidName()){
return false;
}
if(!isValidID()){
return false;
}
return true;
}