Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
64
rated 0 times [  67] [ 3]  / answers: 1 / hits: 11444  / 10 Years ago, sat, april 19, 2014, 12:00:00

I'm trying to change a form input field (username) to uppercase and to remove all spaces within the text. I know there are a few ways one could do this but I need to figure out how to do this using a function. I need to get the input by it's id and use the toUpperCase method but I cannot figure this out for the life of me. As you might be able to tell, I am new to JavaScript. Any help would be greatly appreciated. Thank you.



HTML:



<div class=login-form>
<h1 class=h1-login>Welcome</h1>
<h4>Please login to enjoy our services</h4>

<form class=login-form name=login-form method=post>

<label for=username><span class=required>*</span>Username:</label>
<input id=username type=text name=username autofocus onkeyup=changeToUpperCase()>
<label for=password><span class=required>*</span>Password:</label>
<input type=password name=password size=8 maxlength=8>

<input class=login type=submit value=Login>

</form>

</div>


Here is the JavaScript:



function changeToUpperCase() {
document.login-form.value = document.login-form.value.toUpperCase();
}


This code does not work... and I have no idea how to remove spaces from an input text. Please help me.


More From » html

 Answers
1

I'd change the html to this:



onkeyup=changeToUpperCase(this)


And adjust the function to this



function changeToUpperCase(t) {
var eleVal = document.getElementById(t.id);
eleVal.value= eleVal.value.toUpperCase().replace(/ /g,'');
}


Example


[#45893] Friday, April 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jamaal

Total Points: 515
Total Questions: 102
Total Answers: 107

Location: France
Member since Thu, May 6, 2021
3 Years ago
;