How do I capitalize the first letter of each text input in javaScript?
Possible Duplicate:
Capitalize the first letter of string in JavaScript
到目前为止,这可能是代码。我希望"名字"和"姓氏"字段将第一个字母大写,并使所有其他字母变小:
另外,我不熟悉JavaScript,所以我不确定自己在做什么。
最新编辑。这个代码有什么问题?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <HTML> <HEAD> <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; } </HEAD> <BODY> <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> </BODY> </HTML> |
试试这个:
1 2 3 4 5 6 7 8 9 10 11 | 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); |
对于害怕正则表达式的人(lol):
1 2 3 4 5 6 7 8 9 10 | 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(""); } |
要大写单个字符串,请参阅如何在javascript中将字符串的第一个字母变为大写?.
如果要对字符串中的每个单词执行此操作,则需要将字符串拆分为单词,对每个单词应用
1 2 3 4 5 6 | 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(""); } |
或者您可以在它们上应用regex,以匹配每个第一个字母并替换它们:
1 2 3 4 5 | function titleCase(str) { return str.replace(/\b\S/g, function(c){ return c.toUpperCase(); }); } |
在最新的编辑中:在第三个if条件中有语法错误,它缺少右括号。另外,在函数体之前插入了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /* 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来更正名称。当表单被提交时,您将执行此操作,并且只有当用户将一个输入留空并遇到错误消息时,用户才会看到此操作的效果。
无论如何,您都需要在服务器端执行此操作(从不信任用户输入/发布数据,可能会禁用javascript),因此如果您不熟悉javascript(或不喜欢它或其他),只需省略此"功能"。
"don't know how to put it into my code"
将
1 2 | formObj.FIRST_Name.value = titleCase(formObj.FIRST_Name.value); formObj.LAST_Name.value = titleCase(formObj.LAST_Name.value); |
这样,在提交表单时,将使用您的
(顺便说一下,可以从
试试这个代码:
1 2 3 4 5 6 7 | 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'); |