[英]Remove Spaces and HTML Tags
Trying to make sure no one put a space in the username on sign up, I got the html tags to removed but not spaces. 为了确保没有人在用户名上输入空格,我将html标记删除,但没有空格。
Javascript: 使用Javascript:
$("#user").keypress(function (evt) {
if (isValid(String.fromCharCode(evt.which)))
return false;
});
function isValid(str) {
return /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g.test(str);
}
function stripspaces(input) {
input.value = input.value.replace(/\s/gi, "");
return true;
}
HTML: HTML:
Username: <input type="text" id="user"><br/>
You can see it here. 在这里你可以看到它。 http://jsfiddle.net/QshDd/63/
http://jsfiddle.net/QshDd/63/
You can try with (each space character is being replaced, character by character, with the empty string): 您可以尝试(每个空格字符都被替换为空字符,用空字符串替换):
str = str.replace(/\s/g, '');
or (each contiguous string of space characters is being replaced with the empty string by the "+" character): 或(每个连续的空格字符都由空字符串替换为“ +”字符):
str = str.replace(/\s+/g, '');
Ciao! 再见!
You need to return the input value with the spaces removed: 您需要返回除去空格的输入值:
function stripspaces(input)
{
return input.value.replace(/\s/gi,"");
}
The way you had it written the function just returned true
regardless. 无论如何,编写函数的方式都返回
true
。 Also, it seems you weren't calling the function (admittedly from only a quick read). 另外,似乎您没有在调用该函数(仅通过快速阅读即可)。
Although, it's probably easier to simply add \\s
(a regular expression special character for white-space characters) to the characters to replace, giving: 虽然,将
\\s
(用于空白字符的正则表达式特殊字符)简单地添加到要替换的字符中可能会更容易,从而得到:
function isValid(str) {
return /[~`!#$%\^&*+=\-\[\]\\';,/\s{}|\\":<>\?]/g.test(str);
}
References: 参考文献:
尝试这个,
input.value.replace(/\n/g," ").replace( /<.*?>/g, "" );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.