Javascript jQuery 去除前导和尾随空格

发布于 2024-12-09 12:18:34 字数 981 浏览 0 评论 0原文

我想修剪该值(去掉前导和尾随空格)并使每个单词的第一个字母大写。当用户离开元素(模糊事件)时,

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;
    } 
}

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

I want to trim the value (strip leading and trailing spaces) and make first letter in every word capital. When user leave from the element (blur event)

HTML input as follows

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

JS piece of code

$(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;
    } 
}

Please understand why it isn't working or maybe have a better approach to solve this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我很坚强 2024-12-16 12:18:34
$(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
$(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;
    } 
}

try this must work, only few changes done,

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

to

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

touppercase

to

toUpperCase

tolowercase

yo

toLowerCase
末が日狂欢 2024-12-16 12:18:34

你可以试试这个 $.trim(firstName).replace(/^([az])|\s+([az])/g, function ($1) {return $1.toUpperCase();})

You can try this $.trim(firstName).replace(/^([a-z])|\s+([a-z])/g, function ($1) {return $1.toUpperCase();})

谷夏 2024-12-16 12:18:34

这样就可以了 -

$(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

This would do it -

$(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();
            })
        })
    });
});

Demo - http://jsfiddle.net/ipr101/QvATH/1

屋顶上的小猫咪 2024-12-16 12:18:34

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

$(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/

You had several errors trought the code

$(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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文