[英]Replace line-breaks by spaces using javascript
I want to see if it's possible to block the enter key and replace it with a space. 我想看看是否可以阻止输入键并用空格替换它。 I'm also using form validation to only allow letters, numbers and some other specific characters like the dollar sign,minus, and period and so on.
我也使用表单验证只允许字母,数字和一些其他特定字符,如美元符号,减号和句点等。
Here is that code, I would like to see if I can combine them into one and be able to check for the validation and replace the key press with a space all in one code/call. 这是代码,我想看看我是否可以将它们组合成一个并且能够检查验证并用一个代码/调用中的空格替换按键。
<script type="text/javascript">
function ValidateForm(form)
{
var str
str=document.getElementById('limitedtextarea').value
str=str.replace(/[^A-Za-z0-9.-:/$ ]/g, "");
document.getElementById('limitedtextarea').value=str
//return true;
}
</script>
<FORM action="sms_SendMessage.asp" method=post onsubmit="javascript:return ValidateForm(this)" target=_blank>
Thanks for the help... 谢谢您的帮助...
In javascript, the line-break character is represented by \\n
. 在javascript中,换行符由
\\n
表示。 You could replace them by spaces in your validation function like this : 您可以在验证函数中用空格替换它们,如下所示:
function ValidateForm(form)
{
var str
str=document.getElementById('limitedtextarea').value
str=str.replace(/[^A-Za-z0-9.-:/$ ]/g, "");
str=str.replace(/\n/g, " ");
document.getElementById('limitedtextarea').value=str
//return true;
}
If you remove the onsubmit
and do not have a submit button, typing enter will not submit it. 如果您删除了
onsubmit
并且没有提交按钮,则输入enter将不会提交。
<body>
<form>
<textarea></textarea>
</form>
<script>
(function(){
var txt = document.getElementsByTagName('TEXTAREA')[0];
txt.onkeypress = function(ev){
ev = ev || window.event;
if ( ev.keyCode === 13 ){
if (window.event) {
window.event.returnValue = false;
}
if (ev && ev.preventDefault) {
ev.preventDefault();
}
txt.value += ' ';
}
};
})();
</script>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.