2011-08-09 28 views
15

如何讓用戶在Javascript中的文本框中輸入文本框的所有字符爲小寫字母?如何在Javascript中自動輸入小寫字母

<input type="text" name="thishastobelowercase"> 

感謝您的任何幫助。

+3

嘿@JordashTalon。如果您之前提出的問題的答案中有任何答案,那麼您應該標記答案。這會獎勵那些努力幫助您並讓其他人知道問題得到解答的人。 –

回答

8
$('input').keyup(function(){ 
    this.value = this.value.toLowerCase(); 
}); 
30

我只想讓CSS爲你做這個,而不是用JavaScript周圍胡鬧的:

<input type="text" name="tobelowercase" style="text-transform: lowercase;"> 
+0

+0

對不起,忘記了代碼示例:-) –

+17

重要的是要注意,這隻影響顯示,而不是實際值。換句話說,在用戶界面中,您將看到全部小寫字母,但在服務器端,您將看到使用的任何大寫字符。 –

4

是否只需要在小寫顯示,或是否有小寫?如果你想顯示小寫,你可以使用CSS text-transform: lowercase

無論如何,您需要強制實施這個約束服務器端,因爲用戶可以禁用任何JS代碼來強制它保持小寫。

我的建議:使用CSS text-transform使其始終以小寫顯示,然後在使用前在服務器端執行toLower或您的語言的變體。

14

兩種方式:

使用CSS

.lower { 
    text-transform: lowercase; 
} 

<input type="text" name="thishastobelowercase" class="lower"> 

使用JS

<input type="text" name="thishastobelowercase" onkeypress="this.value = this.value.toLowerCase();"> 
0

這是您要

驗證類型口罩
(function($) { 
    $.fn.maskSimpleName = function() { 
     $(this).css('text-transform', 'lowercase').bind('blur change', function(){ 
      this.value = this.value.toLowerCase(); 
     }); 
    } 
})(jQuery); 

使用它的優點是光標不會進入每個輸入字符的輸入末尾,就像使用鍵控或keupress解決方案時一樣。

$('#myinput').maskSimpleName(); 

這是一種使用它的方法。

Obs:此掩碼也將小寫數據發送到服務器。

-1

引導文本轉換:

<p class="text-lowercase">Lowercased text.</p> 
<p class="text-uppercase">Uppercased text.</p> 
<p class="text-capitalize">CapiTaliZed text.</p> 
相關問題