I'm trying to find a way to validate home names within a script.
Upto know I've used this and it's worked very well.
function home(str) {
if (str.length == 0) {
document.getElementById("home").innerHTML = "";
return;
}
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("home").innerHTML = xmlhttp.responseText;
if (xmlhttp.responseText.indexOf("ok") != -1) {
document.getElementById("submit").disabled = false;
} else {
document.getElementById("submit").disabled = true;
}
}
}
str = str.replace(/#/g, '%23');
xmlhttp.open("GET", "validate.php?home=" + str, true);
xmlhttp.send();
}
On the input field I've added an onkeyup event to call ('home') so each character entered is passed to validate.php and the results returned.
The results are a simple text string which is written to a span with the ID of 'home'. If the return result contains the text 'ok' then the form can be submitted, if not the forms submit is disabled.
All this is working well.
I seem to remember doing this in jquery using an ajax request ! Can anyone advise how I would do that ? so it works the same ?
Thanks