11

Possible Duplicate:
Capitalize the first letter of string in JavaScript

This is may code so far. I would like the FIRST_Name and LAST_Name fields to capitalize the first letter and have all other letters small:

Also, I am unfamiliar with javaScript so I am not exactly sure what I am doing.

latest edit. What is wrong with this code?

    <HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript" type="text/javascript">
<!--
function CheckForm()

formObj.FIRST_Name.value = titleCase(formObj.FIRST_Name.value);
formObj.LAST_Name.value = titleCase(formObj.LAST_Name.value);

function titleCase(str) {
    var words = str.split(/\s+/);
    for (var i=0; i<words.length; i++)
        words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
    return words.join(" ");
}


{
  var formObj = document.getElementById("Data");
  var firstname = formObj.FIRST_Name.value;
  var lastname = formObj.LAST_Name.value;


    if(notEmpty(formObj.FIRST_Name, "Please enter your first name")){       
    if(notEmpty(formObj.LAST_Name,"Please enter your last name")){  
    if(titleCase(formObj.FIRST_Name)            

    return true;}}

    return false;
    }

function notEmpty(elem, helperMsg){
    if(elem.value.length == 0){
        alert(helperMsg);
        elem.focus(); // set the focus to this input
        return false;
    }
    return true;
}


</script>
</HEAD>
<BODY>
<div style="background: #CCCC99">
<HR><FORM id="Data" onsubmit="return CheckForm()" action="post to server" method=post>
<P>First Name: <input type=text name=FIRST_Name maxlength=15 size=15>
   Last Name:  <input type=text name=LAST_Name maxlength=15 size=15></P>
<input type=submit value="Submit Products Registration Form" style="width: 220px"><input type=reset value="Reset">
</form>
</div>
</BODY>
</HTML>
4

5 回答 5

6

尝试这个:

String.prototype.toCap = function()
{ 
   return this.toLowerCase().replace(/^.|\s\S/g, function(str) { 
       return str.toUpperCase(); 
   });
}        

var firstname = "gob".toCap();
var lastname = "bluth".toCap();

alert(firstname + ' ' + lastname);
于 2012-07-26T02:00:38.857 回答
4

对于我们这些害怕正则表达式的人(大声笑):

function titleCase(str)
{
    var words = str.split(" ");
    for ( var i = 0; i < words.length; i++ )
    {
        var j = words[i].charAt(0).toUpperCase();
        words[i] = j + words[i].substr(1);
    }
    return words.join(" ");
}
于 2012-07-26T01:57:38.703 回答
3

试试这个代码:

function toTitleCase(str){
    var words = str.match(/\w*/g);
    for(var i=0;i<words.length;i++)
        words[i] = words[i][0].toUpperCase() + words[i].slice(1);
    return words.join(' ');
}
toTitleCase('my name');
于 2012-07-26T01:48:39.270 回答
3

“不知道如何将其放入我的代码中”

titleCase()(或您现在正在调用的任何内容)函数复制到您的脚本块中,然后在函数中的语句toTitleCase()之前添加以下代码:ifCheckForm()

formObj.FIRST_Name.value = titleCase(formObj.FIRST_Name.value);
formObj.LAST_Name.value = titleCase(formObj.LAST_Name.value);

这样,在提交表单时,字段将使用您的toTitleCase()函数进行更改。(如果您在if语句之前添加代码,则无论notEmpty()验证是否通过,字段都会更改,这对我来说似乎是合理的。)

(顺便说一句,可以从函数中删除firstnameand变量声明,因为您从不使用这些变量。)lastnameCheckForm()

于 2012-07-26T02:18:49.433 回答
3

要将单个字符串大写,请参阅如何在 JavaScript 中将字符串的第一个字母变为大写?.

如果要对字符串中的每个单词执行此操作,则需要将字符串拆分为单词,将ucFirst函数应用于每个单词并将它们重新连接在一起:

function titleCase(str) {
    var words = str.split(/\s+/);
    for (var i=0; i<words.length; i++)
        words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
    return words.join(" ");
}

或者您可以对它们应用正则表达式,以匹配每个第一个字母并替换它们:

function titleCase(str) {
    return str.replace(/\b\S/g, function(c){
        return c.toUpperCase();
    });
}

在您最近的编辑中:您在第三个 if 条件中遇到语法错误,它缺少右括号。此外,您已经titleCase在函数体之前插入了代码。

/* the titleCase function from above here */

function CheckForm() {
    var formObj = document.getElementById("Data");
    var firstname = formObj.FIRST_Name;
    var lastname = formObj.LAST_Name;

    firstname.value = titleCase(firstname.value);
    lastname.value = titleCase(lastname.value);

    return notEmpty(firstname, "Please enter your first name")
      && notEmpty(lastname, "Please enter your last name");
}

function notEmpty(elem, helperMsg){
    if(elem.value.length == 0){
        alert(helperMsg);
        elem.focus(); // set the focus to this input
        return false;
    }
    return true;
}

我个人对此的看法:根本不要尝试用 JavaScript 更正名称。您在提交表单时执行此操作,并且仅当用户将一个输入留空并遇到错误消息时,用户才会看到此操作的效果。

无论如何,您都需要在服务器端执行此操作(永远不要相信用户输入/ POST 数据,javascript 可能会被禁用),所以如果您不熟悉 JavaScript(或不喜欢它或其他什么),请忽略此“功能”。

于 2012-07-26T02:28:34.643 回答