使用 javascript 将换行符替换为空格
我想看看是否可以阻止回车键并将其替换为空格。我还使用表单验证仅允许字母、数字和其他一些特定字符,例如美元符号、减号和句点等。
这是该代码,我想看看是否可以将它们合并为一个,并能够检查验证并将按键替换为一个代码/调用中的空格。
<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>
感谢您的帮助...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 JavaScript 中,换行符用
\n
表示。您可以在验证函数中用空格替换它们,如下所示:In javascript, the line-break character is represented by
\n
. You could replace them by spaces in your validation function like this :如果您删除了
onsubmit
并且没有提交按钮,则输入 Enter 将不会提交。If you remove the
onsubmit
and do not have a submit button, typing enter will not submit it.