getting the first 6 numbers from an input with javascript

BR
BR used Ask the Experts™
on
Dear experts,
<input type="tel" name="number" id="number" required>

Open in new window


when user leave the input area, I want to get the first 6 numbers of the input and put it into str variable.
How can I do that? thank you

<script>
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        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 (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
		document.getElementById("seh").innerHTML = str;
		
    }
}
</script>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Director of Technology Development, IPM
Commented:
Use the onblur() method:

<input type="tel" name="number" id="number" onblur="getdigits();" required>

Open in new window

<script language="javascript">
function getdigits() {
    var telnum = document.getElementById("number").value;
   alert(telnum.substring(0,6));
    showUser(telnum.substring(0,6));
}

function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        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 (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getuser.php?q="+str,true);
        xmlhttp.send();
		document.getElementById("seh").innerHTML = str;
		
    }
}
</script>

Open in new window

BRDigital Marketing

Author

Commented:
Dear Sam Jacobs
you are answer is perfect. thank you so much
Sam JacobsDirector of Technology Development, IPM

Commented:
You are most welcome.
leakim971Multitechnician
Top Expert 2014

Commented:
use :
var six = yourstring.substr(0,6);

<input type="tel" name="number" id="number" required onblur="showUser(this.value.substr(0,6))">

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial