我已经成功地做了其他事情:
-
使用javascript阻止第一个字符为空格
-
使用javascript阻止连续空格
-
使用html强制输入中的每个单词大写
现在我想强制第一个字符为字母,以防止仅在第一个字符中出现数字和特殊字符。
我知道这是可以做到的,但我找不到这个有用的代码。我已经做了这么多,这是我的状态最不需要的!:D
这就是我做上述所有事情的方式:
function SpaceBlock() {
var space = document.getElementById("real_name");
if(space.selectionStart === 0 && window.event.code === "Space"){
window.event.preventDefault(); } }
function SpaceBlock2() {
var space = document.getElementById("display_name");
if(space.selectionStart === 0 && window.event.code === "Space"){
window.event.preventDefault();} }
var lastkey;
var ignoreChars = ' \r\n'+String.fromCharCode(0);
function ignoreSpaces(e) {
e = e || window.event;
var char = String.fromCharCode(e.charCode);
if(ignoreChars.indexOf(char) >= 0 && ignoreChars.indexOf(lastkey) >= 0) {
lastkey = char;
return false; }
else {
lastkey = char;
return true; } }
var lastkey2;
var ignoreChars2 = ' \r\n'+String.fromCharCode(0);
function ignoreSpaces2(e) {
e = e || window.event;
var char2 = String.fromCharCode(e.charCode);
if(ignoreChars2.indexOf(char2) >= 0 && ignoreChars2.indexOf(lastkey2) >= 0) {
lastkey2 = char2;
return false; }
else {
lastkey2 = char2;
return true; } }
<input type="text" name="real_name" placeholder="Real Name" id="real_name" required minlength="6" maxlength="24" tabindex="1" onkeydown="SpaceBlock()" onkeypress="return ignoreSpaces(event);" style="text-transform: capitalize;" >
<input type="text" name="display_name" placeholder="Display Name" id="display_name" required minlength="6" maxlength="24" tabindex="1" onkeydown="SpaceBlock()" onkeypress="return ignoreSpaces(event);" style="text-transform: capitalize;" >
这是我表格中最不需要的东西,只是希望有人能帮忙。谢谢!