0

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

1

1 Answer 1

1

Something like this (not tested):

function home(str){
    if (str.length == 0) {
        $("#home").html('');
        return;
    }
    str = str.replace(/#/g, '%23');
    $.get("validate.php?home=" + str, function(data){
        $("#home").html(data);
        $("#submit").attr('disabled',data.indexOf("ok") != -1);
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.