Change input value in form with jQuery

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Change input value in form with jQuery

kingen
Hi!

I'm trying to help my user when they use my forms. The things I would love to do is to trim the value (strip leading and trailing spaces) and make first letter in every word capital. This is what I've came up with so far.

HTML

<input id="user_forenamne" name="user_forenamne" type="text"/>

Javascript

$(document).ready(function(){
        var forename = $("#user_forenamne");
       
        forename.blur(validateForename);
       
        function validateForename(){
                var name = $("user_forename").val;
                //strip leading and trailing spaces
                name = $.trim(name)
                //change first letter in every word to uppercase
                name = Capital(name);
                //update input field whit new value
                $("#user_forename").val("name");
        }
});
function Capital(value) {
  if (value != "") {
    var firstLetter = value.substring(0, 1).toUpperCase();
    var restOfWord = value.substring(1, value.length).toLowerCase();
    value = firstLetter + restOfWord;
  }

Does anyone understand why it isn't working or maybe have a better approach to sole this problem.

Many thanks in advance
Adam Nyberg
       
Reply | Threaded
Open this post in threaded view
|

Re: Change input value in form with jQuery

Heena
kingen wrote
Hi!

I'm trying to help my user when they use my forms. The things I would love to do is to trim the value (strip leading and trailing spaces) and make first letter in every word capital. This is what I've came up with so far.

HTML

<input id="user_forenamne" name="user_forenamne" type="text"/>

Javascript

$(document).ready(function(){
        var forename = $("#user_forenamne");
       
        forename.blur(validateForename);
       
        function validateForename(){
                var name = $("user_forename").val;
                //strip leading and trailing spaces
                name = $.trim(name)
                //change first letter in every word to uppercase
                name = Capital(name);
                //update input field whit new value
                $("#user_forename").val("name");
        }
});
function Capital(value) {
  if (value != "") {
    var firstLetter = value.substring(0, 1).toUpperCase();
    var restOfWord = value.substring(1, value.length).toLowerCase();
    value = firstLetter + restOfWord;
  }

Does anyone understand why it isn't working or maybe have a better approach to sole this problem.

Many thanks in advance
Adam Nyberg
       

Can you please put your problem in detail ?

Reply | Threaded
Open this post in threaded view
|

Re: Change input value in form with jQuery

Ridima
In reply to this post by kingen

kingen wrote
Hi!

I'm trying to help my user when they use my forms. The things I would love to do is to trim the value (strip leading and trailing spaces) and make first letter in every word capital. This is what I've came up with so far.

HTML

<input id="user_forenamne" name="user_forenamne" type="text"/>

Javascript

$(document).ready(function(){
        var forename = $("#user_forenamne");
       
        forename.blur(validateForename);
       
        function validateForename(){
                var name = $("user_forename").val;
                //strip leading and trailing spaces
                name = $.trim(name)
                //change first letter in every word to uppercase
                name = Capital(name);
                //update input field whit new value
                $("#user_forename").val("name");
        }
});
function Capital(value) {
  if (value != "") {
    var firstLetter = value.substring(0, 1).toUpperCase();
    var restOfWord = value.substring(1, value.length).toLowerCase();
    value = firstLetter + restOfWord;
  }

Does anyone understand why it isn't working or maybe have a better approach to sole this problem.

Many thanks in advance
Adam Nyberg
       



Ya i agree with Heena, will you please elaborate your question/problem.