[英]RegExp for Blank, Tabs or blank spaces in textbox
I need the help of the experts on this forum. 我需要这个论坛上专家的帮助。
I would like to come up with a RegEx (javascript) to validate a textbox which has any blank spaces in it, any blank tabs in it, and also check for an empty textbox? 我想提出一个RegEx(javascript)来验证其中有任何空格,任何空白标签的文本框,并检查是否有空的文本框?
ie. 即。
alert("Accepted")
------------------------------
Jameswithsometext
Billy Jean
alert("Not accepted")
------------------------------
- inadvertenly typed blank spaces
- inadvertenly typed tabs
- blank textbox
\\s
should match any whitespace character. \\s
应该匹配任何空格字符。 So if that's all there is, you have a problem. 所以,仅此而已,您就会遇到问题。
var textbox = document.getElementById('mytextbox');
if (/^\s*$/.test(textbox.value)) {
alert('Come on man, seriously, what the hell are you doing?');
} else {
alert('Accepted');
}
See it here: http://jsfiddle.net/68KzX/ 在这里查看: http : //jsfiddle.net/68KzX/
^
start of string ^
字符串的开头 \\s
any whitespace character \\s
任何空格字符 *
match any number of characters *
匹配任意数量的字符 $
end of string $
字符串结尾 So this will match if the string has zero characters, or if it has only whitespace characters. 因此,如果字符串包含零个字符,或者仅包含空格字符,则此匹配。
Using /^\\S+$/
(at least one character of non-whitespace) we can do, 使用
/^\\S+$/
(至少一个非空白字符),我们可以做到,
var p = prompt("Enter text", "");
var rx = /^\S+$/;
if (rx.test(p) == false)
alert("Not accepted");
else
alert("Accepted");
Note that this also handles the case of nothing entered. 注意,这也可以处理不输入任何内容的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.