Only one word on <input

Hi I have a form on one of my pages for entering in information such as names etc

I have some code that will make sure the first letter is always capitalised and the rest lowercase.
What I want to do is expand upon this and use substr to remove everything after the first space.
So if a user goes to enter in the first name field "John Smith" you would only get John

Below is current code
<input onblur="this.value = this.value.substr(0, 1).toUpperCase() + this.value.substr(1).toLowerCase();" type="text" name="last_name" value="lastname" size="10">

Open in new window

Who is Participating?
ludofulopConnect With a Mentor Commented:
<input onblur="this.value = this.value.substr(0, 1).toUpperCase() + this.value.substr(1).toLowerCase(); this.value = this.value.split(' ')[0];" type="text" name="last_name" value="lastname" size="10">
Would something like this work for you?
<script type="text/javascript">
function func(a){
	a = a.substr(0, 1).toUpperCase() + a.substr(1).toLowerCase();	
	a = a.split(' ')
	document.getElementById('test').value = a[0];
<input onblur="func(this.value);" type="text" name="last_name" value="lastname" size="10" id="test">

Open in new window

Pratima PharandeCommented:
Try this also

<input onblur="this.value =this.value.substr(0, 1).toUpperCase() + this.value.substr(1).toLowerCase().substr(0,this.value.indexOf(' '));" type="text" name="last_name" value="lastname" size="10">
alex4544Author Commented:
This works perfectly for what I was trying to achieve although the others have their merits this was first and easiest to implement
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.