Link to home
Start Free TrialLog in
Avatar of BR
BRFlag for Türkiye

asked on

getting the first 6 numbers from an input with javascript

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

ASKER CERTIFIED SOLUTION
Avatar of Sam Jacobs
Sam Jacobs
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BR

ASKER

Dear Sam Jacobs
you are answer is perfect. thank you so much
You are most welcome.
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