以下代碼用於檢查numbers.num1,num2和num3的驗證。驗證工作如num1小於num2小於num3。此訂單隨後會生效,但在顯示提示消息後,輸入的號碼和打破訂單的號碼需要重置爲空。對數字排序的javascript驗證
<form name="validateForm" method="POST">Enter num1
<input type="text" id="num1" name="num1" value="" onchange="javascript:validate()" />
<br/>Enter num2
<input type="text" id="num2" name="num2" value="" onchange="javascript:validate()" />
<br/>Enter num3
<input type="text" id="num3" name="num3" value="" onchange="javascript:validate()" />
<br/>
</form>
代碼:
function validate() {
var num1 = document.validateForm.num1.value;
var num2 = document.validateForm.num2.value;
var num3 = document.validateForm.num3.value;
var count = 0;
if (num1 != "") {
count++
}
if (num2 != "") {
count++
}
if (num3 != "") {
count++
}
var numarray = new Array(count);
var flag = "false";
var j = 0;
if (!isNaN(num1)) {
numarray[j] = num1;
} else {
flag = "true";
}
if (!isNaN(num2)) {
if (flag == "true") {
alert("numbers are not in order");
document.validateForm.num2.value = "";
return false;
}
numarray[j] = num2;
j++;
} else {
flag = "true";
}
if (!isNaN(num3)) {
if (flag == "true") {
alert("numbers are not in order");
document.validateForm.num3.value = "";
return false;
}
numarray[j] = num3;
j++;
}
if (numarray.length > 1) {
for (var x = 0; x < numarray.length - 1; x++) {
if (numarray[x + 1] < numarray[x]) {
alert("numbers are not in order");
return false;
}
}
}
}
你看了,直到其最終的問題嗎?據我所知,這是已經工作的部分。問題與警報消息/變量重置有關。 – Trinimon