Only one word on <input

Posted on 2009-02-13
Last Modified: 2013-11-19
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

Question by:alex4544
    LVL 15

    Accepted 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">
    LVL 15

    Expert Comment

    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

    LVL 39

    Expert Comment

    by:Pratima Pharande
    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">
    LVL 2

    Author Closing Comment

    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

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Suggested Solutions

    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
    The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now