[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

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

1 Solution
<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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now