我正在使用此代码来验证邮政编码。该代码就像一个魅力,但我想在提交之前验证输入并确保输入仅为数字。我将此添加到代码中:
Code1- 验证数字输入的代码
function validateForm() {
var x = document.forms["form2"]["postal"].value;
if(/[^0-9]+$/.test(x)) {
alert("The postcode must have ONLY numeric characters - Please go to contact page if you live outside Australia.");
myformName.myformField.focus();
return false;
}
}
但不幸的是它不起作用。有没有人可以帮我解决这个问题?我想知道如何将上面的代码集成到下面的代码中:
code2 - 主要代码
$(document).ready(function () {
$('#tab-container').easytabs();
});
var xmlHttp;
function check() {
xmlHttp = GetXmlHttpObject();
if(xmlHttp == null) {
alert("Browser does not support HTTP Request");
return;
}
var code = document.getElementById("postal").value;
document.getElementById("loader4").style.visibility = 'visible';
var url = "Ajax/check_code.php";
url = url + "?p=" + code;
//alert(code);
xmlHttp.onreadystatechange = stateChanged;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
function stateChanged() {
if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
//alert(xmlHttp.responseText);
if(xmlHttp.responseText == 1) {
//alert(xmlHttp.responseText);
//document.getElementById("pro").disabled = false;
window.location.href = 'booking.html';
} else if(xmlHttp.responseText == 0) {
document.getElementById("results").style.visibility = 'visible';
document.getElementById("results").innerHTML = 'Alert Message';
}
document.getElementById("loader4").style.visibility = 'hidden';
}
}
}
function GetXmlHttpObject() {
var xmlHttp = null;
try {
// Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch(e) {
//Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
这是HTML:
<form name="form2" onsubmit="return validateForm();">
<label>STEP 1:</label>
<label>Enter Your Post Code</label>
<input type="text" name="postal" placeholder="e.g. 4000" id="postal" />
<input type="button" class="call-to-action" name="pro" id="pro" onclick="check()" value="Book Now" />
</form>
所以我想再次在 code2 中添加 code1 并获得数字验证。