这是我的HTML代码,问题出在Check函数中。当我不输入数字或输入大于10且小于0的数字时,我希望弹出警报。 相反,当我给它一封信时,它会计算出平均值并给出我不想要的NaN。这是我的代码:
MainActivity
答案 0 :(得分:0)
虽然需要重构,但这应该有效。基本思想是将验证包装在一个函数中,然后在验证函数失败时继续询问提示。
function closeWin() {
myWindow.close();
}
function trimfield(str)
{
return str.replace(/^\s+|\s+$/g,'');
}
function Check()
{
var counter=0;
var lesson = new Array();
getNum(0, "1st Lesson Grade");
getNum(1, "2nd Lesson Grade");
getNum(2, "3rd Lesson Grade");
getNum(3, "4th Lesson Grade");
getNum(4, "5th Lesson Grade");
getNum(5, "6th Lesson Grade");
x = parseFloat(lesson[0]);
x1 = parseFloat(lesson[1]);
x2 = parseFloat(lesson[2]);
x3 = parseFloat(lesson[3]);
x4 = parseFloat(lesson[4]);
x5 = parseFloat(lesson[5]);
avrg = (x + x1 + x2 + x3 + x4 + x5)/6;
var reg = document.getElementById('srn').value;
var name = document.getElementById('fn').value;
var lastname = document.getElementById('ln').value;
var i;
var myWindow = window.open("", "Grade", "width=500,height=500");
myWindow.document.write("<h3>The average is: " + avrg + "<br>" + "First Name: " + name + "<br>"+ "Last Name: " + lastname + "<br>"+ "Studen Registration Number: " + reg + "</h3>");
function checkNum(n){
var hasError = false;
if(isNaN(n)){
hasError = true;
counter=1;
}
else if(trimfield(n) == '') {
hasError = true;
n.focus();
counter=1;
}
else if(n<0 || n>10){
hasError = true;
counter=1;
} else{
counter=0;
}
if(counter==1){
alert("Wrong Info")
}
return !hasError;
}
function getNum(index, msg){
temp = window.prompt(msg, "0");
if(!checkNum(temp)){
getNum(index, msg);
} else{
lesson[index] = temp;
}
}
}
<fieldset>
<legend>Personal Info:</legend>
<form>
<label for="fn">First Name:</label>
<input id="fn" type="text" name="fn" ><br><br>
<label for="ln">Last Name: </label>
<input id="ln" type="text" name= "ln"><br><br>
<label for="srn">Student Registration Number: </label>
<input id="srn" type="text" name= "srn" maxlength="6" placeholder="ex. 'P16***'"><br><br>
Semester: <select name="semester">
<optgroup label="1st Year">
<option>1st Semester</option>
<option>2nd Semester</option>
</optgroup>
<optgroup label="2nd Year">
<option>3rd Semester</option>
<option>4th Semester</option>
</optgroup>
<optgroup label="3rd Year">
<option>5th Semester</option>
<option>6th Semester</option>
</optgroup>
<optgroup label="4th Year">
<option>7th Semester</option>
<option>8th Semester</option>
</optgroup>
</select><br><br>
<input type= "button" value= "Continue" onClick="Check();" >
<input type="reset" value="Cancel">
<button onclick="closeWin()">Close Window</button>
</form>
</fieldset>
答案 1 :(得分:0)
我的变体&#34; onfly-validation&#34;
function closeWin() {
myWindow.close();
}
function trimfield(str) {
return str.replace(/^\s+|\s+$/g, '');
}
function Check() {
var counter = 0;
var lessonsCount = 0;
var lesson = []; // same as = new Array()
lesson[0] = window.prompt("1st Lesson Grade", "0"); // recommended do this in for-loop
lesson[1] = window.prompt("2nd Lesson Grade", "0");
lesson[2] = window.prompt("3rd Lesson Grade", "0");
lesson[3] = window.prompt("4th Lesson Grade", "0");
lesson[4] = window.prompt("5th Lesson Grade", "0");
lesson[5] = window.prompt("6th Lesson Grade", "0");
lessonsCount = lesson.length;
var x = [];
var sum = 0;
//* onfly-validation
for (var j = 0; j < lessonsCount; j++) {
x[j] = parseFloat(lesson[j]);
if (isNaN(x[j])) {
alert("Wrong grade for lesson " + (j + 1));
return; // got NaN - stop calculation
} else if (x[j] < 0 || x[j] > 10) {
alert("Grade for lesson " + (j + 1) + " should be in 0..10");
return; // wrong number - stop calculation
} else {
sum += x[j];
}
}
avrg = sum / lessonsCount; // if you decide add some lessons - you shouldn't fix lessons number in all your code
var reg = document.getElementById('srn').value;
var name = document.getElementById('fn').value;
var lastname = document.getElementById('ln').value;
var i;
var hasError = false;
var myWindow = window.open("", "Grade", "width=500,height=500");
myWindow.document.write("<h3>The average is: " + avrg + "<br>" + "First Name: " + name + "<br>" + "Last Name: " + lastname + "<br>" + "Studen Registration Number: " + reg + "</h3>");
/*
for(i=0; i<6;i++){
if(!isNaN(lesson[i].value)){
hasError = true;
counter=1;
}
else if(trimfield(lesson[i].value) == '') {
hasError = true;
lesson[i].focus();
counter=1;
}
else if(lesson[i].value<0 || lesson[i].value>10){
hasError = true;
counter=1;
} else
counter=0;
}
if(counter==1){
alert("Wrong Info")
}
return !hasError;
*/
}
&#13;
<fieldset>
<legend>Personal Info:</legend>
<form>
<label for="fn">First Name:</label>
<input id="fn" type="text" name="fn" ><br><br>
<label for="ln">Last Name: </label>
<input id="ln" type="text" name= "ln"><br><br>
<label for="srn">Student Registration Number: </label>
<input id="srn" type="text" name= "srn" maxlength="6" placeholder="ex. 'P16***'"><br><br>
Semester: <select name="semester">
<optgroup label="1st Year">
<option>1st Semester</option>
<option>2nd Semester</option>
</optgroup>
<optgroup label="2nd Year">
<option>3rd Semester</option>
<option>4th Semester</option>
</optgroup>
<optgroup label="3rd Year">
<option>5th Semester</option>
<option>6th Semester</option>
</optgroup>
<optgroup label="4th Year">
<option>7th Semester</option>
<option>8th Semester</option>
</optgroup>
</select><br><br>
<input type= "button" value= "Continue" onClick="Check();" >
<input type="reset" value="Cancel">
<button onclick="closeWin()">Close Window</button>
</form>
</fieldset>
&#13;