0

I believe it's a new request from what I've seen here. I'm using onkeyup() on one input to append and create a new value in a second input. It works great but I want to restrict white/blank spaces in the second input. See pen here

HTML

 <input type="text" id="fullnames" onkeyup="createUsername();" placeholder="Enter Your Full Name">

<!--restrict blank space-->
<input type="text" id="username" placeholder="Username">

Script

    function createUsername() {
    var x = document.getElementById("fullnames").value;
    document.getElementById("username").value= x;
}

Final result

This is how it should be

Full Names : John Doe 
Username : johndoe

2 Answers 2

2

You can use String.prototype.replace and String.prototype.toLowerCase methods:

var name = ' John Doe ';
var username = name.replace(/ /g, '').toLowerCase();

console.log(username);

Btw be aware that there can be more characters that you might want to handle too. For example in my language we can have those in name ěščřžýáíé (plus their upper case variants).

See this gist for more advanced way that can handle those (it does slightly different thing - putting dashes instead of spaces, but its almost the same):

https://gist.github.com/Majkl578/947036

Sign up to request clarification or add additional context in comments.

Comments

0

You could try this:

function createUsername() {
    var x = document.getElementById("fullnames").value;
    x = x.trim() // gets rid of leading and trailing spaces
    var username = document.getElementById("username");
    username.value = x;
}

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.