Javascript jQuery条带前导和尾随空格

时间:2011-10-13 10:18:01

标签: javascript jquery trim

我想修剪值(条带前导和尾随空格)并在每个单词的首字母中加上第一个字母。当用户离开元素时(模糊事件)

HTML输入如下

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

JS一段代码

$(document).ready(function(){ 
     var iptFirstName = $("#iptFirstName");
     iptFirstName.blur(validateForename);
});


function validateForename(){
    var firstName= $("#iptFirstName").val;
    //strip leading and trailing spaces
    firstName= $.trim(firstName)
    //change first letter in every word to uppercase
    firstName= Capital(firstName);
    //update input field whit new value
    $("#iptFirstName").val(firstName);
}

function Capital(eleValue) {
    var eleValue;

    if (eleValue != "") {
        var firstLetter = eleValue.substring(0, 1).touppercase();
        var restOfWord = eleValue.substring(1, eleValue.length).tolowercase();
        eleValue = firstLetter + restOfWord;
        return eleValue;
    } 
}

请理解为什么它不起作用或者可能有更好的方法来解决这个问题。

4 个答案:

答案 0 :(得分:1)

$(document).ready(function(){ 
     var iptFirstName = $("#iptFirstName");
     iptFirstName.blur(validateForename);
});


function validateForename(){
    var firstName= $("#iptFirstName").val();
    //strip leading and trailing spaces
    firstName= $.trim(firstName)
    //change first letter in every word to uppercase
    firstName= Capital(firstName);
    //update input field whit new value
    $("#iptFirstName").val(firstName);
}

function Capital(eleValue) {
    var eleValue;

    if (eleValue != "") {
        var firstLetter = eleValue.substring(0, 1).toUpperCase();
        var restOfWord = eleValue.substring(1, eleValue.length).toLowerCase();
        eleValue = firstLetter + restOfWord;
        return eleValue;
    } 
}

尝试这一点必须有效,只做了一些改动,

var firstName= $("#iptFirstName").val;

var firstName= $("#iptFirstName").val();

touppercase

toUpperCase

tolowercase

toLowerCase

答案 1 :(得分:1)

这样做 -

$(document).ready(function() {
    var iptFirstName = $("#iptFirstName");
    iptFirstName.blur(function() {
        $(this).val(function() {
            return $.trim($(this).val()).replace(/(^[a-z]| [a-z])/g, function($0) {
                return $0.toUpperCase();
            })
        })
    });
});

演示 - http://jsfiddle.net/ipr101/QvATH/1

答案 2 :(得分:1)

您可以尝试此$.trim(firstName).replace(/^([a-z])|\s+([a-z])/g, function ($1) {return $1.toUpperCase();})

答案 3 :(得分:0)

您在代码中遇到了几个错误

$(document).ready(function(){
     $("#iptFirstName").blur(validateForename);
});


function validateForename(){
    var firstName= $("#iptFirstName").val();
    //strip leading and trailing spaces
    firstName= $.trim(firstName);
    //change first letter in every word to uppercase
    firstName= Capital(firstName);      
    //update input field whit new value
    $("#iptFirstName").val(firstName);
}

function Capital(eleValue) {
    if (eleValue != "") {
        var firstLetter = eleValue.substring(0, 1).toUpperCase();
        var restOfWord = eleValue.substring(1, eleValue.length).toLowerCase();
        eleValue = firstLetter + restOfWord;
    }
    return eleValue;    
}

http://jsfiddle.net/b3XhL/