Best way to alphanumeric check in JavaScript

t0mcat picture t0mcat · Dec 13, 2010 · Viewed 279.9k times · Source

What is the best way to perform an alphanumeric check on an INPUT field in JSP? I have attached my current code

function validateCode() {
    var TCode = document.getElementById("TCode").value;

    for (var i = 0; i < TCode.length; i++) {
        var char1 = TCode.charAt(i);
        var cc = char1.charCodeAt(0);

        if ((cc > 47 && cc < 58) || (cc > 64 && cc < 91) || (cc > 96 && cc < 123)) {
        } else {
            alert("Input is not alphanumeric");
            return false;
        }
    }

    return true;
}

Answer

Mahesh Velaga picture Mahesh Velaga · Dec 13, 2010

You can use this regex /^[a-z0-9]+$/i