不完全是問題的答案,但這會限制鍵入時文本區域中的字符數/向下計數。
還刪除任何不需要的字符。
CSS:
<style>
div.textarea-wrap{
padding: 8px 0px;
border-radius: 3px;
postion:relative;
margin: 5px 0px 15px 0px;
}
textarea#styled_text_area {
width: 93.5%;
height: 120px;
font-size: 12px;
border: 1px solid slategrey;
border-radius: 3px;
padding: 8px 3%;
font-family: Tahoma, sans-serif;
background: #E3E9ED;
}
.remaining {
margin: 3px 0px;
line-height: 10px;
}
div.remaining p{
color: lightslategrey;
float: right;
margin: 4px 3% 0px 0px;
float:right;
}
.remaining input{
width:27px;
border: 0;
padding: 0;
border-radius: 0;
background: none;
color: lightslategrey;
float:right;
margin: 0;
}
</style>
的Javascript:
<script>
function clean(el){
var textfield = document.getElementById(el);
var regex = /[^a-z 0-9?!.,]/gi;
if(textfield.value.search(regex) > -1) {
textfield.value = textfield.value.replace(regex, "");
}
}
// Text counter
var maxAmount = <?php echo($message_input_size);?>;
function textCounter(textField, showCountField) {
if (textField.value.length > maxAmount) {
textField.value = textField.value.substring(0, maxAmount);
} else {
showCountField.value = maxAmount - textField.value.length;
}
}
</script>
HTML:
<div class="textarea-wrap">
<textarea name="ta" id="styled_text_area" rows="6" placeholder="Type here..." onKeyDown="textCounter(this.form.ta,this.form.countDisplay), clean('styled_text_area');" onKeyUp="textCounter(this.form.ta,this.form.countDisplay), clean('styled_text_area');"></textarea>
<div class="remaining">
<p>Characters Remaining</p>
<input readonly type="text" name="countDisplay" size="3" maxlength="3" value="500"><br><br>
</div>
</div>
如果什麼用戶將文本粘貼到文本區域使用右鍵 - >粘貼? – techfoobar 2012-01-29 11:06:57