如何使用 javascript 正则表达式检查“空”表单字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2249460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use javascript regex to check for "empty" form fields?
提问by Michael Mao
I am working on a php+javascript based project and have already made up a mockup page at : my website
我正在开发一个基于 php+javascript 的项目,并且已经在以下位置制作了一个模型页面: 我的网站
I knew how to use javascript or php to check whether a particular field of form is "empty"or not, that is, whether it contains alphanumerical characters other than whitepsace characters(for instance, space, tab and newline).
我知道如何使用 javascript 或 php 来检查表单的特定字段是否为“空”,即它是否包含除空白字符以外的字母数字字符(例如,空格、制表符和换行符)。
However, my normal apporach no longer works since the jquery plugin that I am using now relies on regex to validate the fields.
但是,由于我现在使用的 jquery 插件依赖于正则表达式来验证字段,因此我的普通 apporach 不再有效。
If you go to the third tab(3. Fill up Shipping Info and Make Payment), you can enter something into the Firstname field and it does the check automatically. Fine. However, if you just simply put some space characters there and jump to the next field, well, it still feels okay for that, which is not correct since no one's first name is nothing!
如果您转到第三个选项卡(3。填写运输信息并付款),您可以在名字字段中输入一些内容,它会自动进行检查。美好的。但是,如果您只是简单地在那里放一些空格字符并跳转到下一个字段,那么感觉还是可以的,这是不正确的,因为没有人的名字什么都不是!
The problem? At the back it has a regex like this :
问题?在后面它有一个这样的正则表达式:
"noSpecialCaracters":{
"regex":"/^[0-9a-zA-Z ]+$/",
"alertText":"* No special caracters allowed"},
This would not filter out empty characters.
这不会过滤掉空字符。
I searched online and tried my best to make up another regex to match, I tried
我在网上搜索并尽力弥补另一个正则表达式来匹配,我试过了
"regex":"/^[^]+$/"
for matching non-empty characters, but that will not do...
用于匹配非空字符,但这不会...
Can anyone help me out? Many thanks in advance!
谁能帮我吗?提前谢谢了!
回答by Nick Craver
Try this for non-whitespace:
试试这个非空白:
([^\s]*)
Example:
例子:
/([^\s])/.test(" A"); //TRUE
/([^\s])/.test(" "); //FALSE
回答by anonymous
function anyChar(str){
return /\S+/.test(str);
}
will return false if emty data
如果 emty 数据将返回 false
回答by Charles Wood
I'm using
我正在使用
/^\s*\S+.*/
which means
意思是
- zero or more whitespace characters (
\s*), followed by - one or more non-whitespace characters (
\S+), followed by - anything at all, whitespace or not (
.*).
- 零个或多个空白字符 (
\s*),后跟 - 一个或多个非空白字符 (
\S+),后跟 - 任何东西,空格与否(
.*)。
This allows a single word or multiple words. I'm allowing whitespace at the beginning because I know how easy it is to miss a single space at the beginning and be really confused as to why your input isn't allowed :|
这允许单个词或多个词。我在开头允许空格,因为我知道在开头错过一个空格是多么容易,并且对为什么不允许您的输入感到非常困惑:|
The Mozilla Developer Network has a great JavaScript regex pagefor reference.
Mozilla 开发人员网络有一个很棒的JavaScript 正则表达式页面供参考。
回答by too much php
To answer your question, the minimal regex is /\S/which will match as long as there is at least one non-whitespace character.
为了回答您的问题,/\S/只要有至少一个非空白字符,最小的正则表达式就会匹配。
However, you probably don't want someone to put in a first name of '12345' or '!!!', so it might be better to use /[a-z]/ias this regex will only match if there is at least one alphabetical character.
但是,您可能不希望有人输入“12345”或“!!!”的名字,因此最好使用/[a-z]/i此正则表达式,因为此正则表达式仅在至少有一个字母字符时才匹配。
回答by Nick Bedford
You may want to try wrapping your expression in the following ^\s*(expression)\s*$. Then use the groups to find the "trimmed" matches. This eliminates only trailing or leading whitespace.
您可能想尝试在以下内容中包装您的表达式^\s*(expression)\s*$。然后使用组来查找“修剪过的”匹配项。这仅消除了尾随或前导空格。
You can force the user to enter trimmed text or you can gracefully accept untrimmed input (better) as I find copying and pasting text often leaves some trailing or leading whitespace which the user may be unaware of.
您可以强制用户输入修剪后的文本,或者您可以优雅地接受未修剪的输入(更好),因为我发现复制和粘贴文本通常会留下一些用户可能不知道的尾随或前导空格。
回答by Charles Ma
/^\s*[0-9a-zA-Z][0-9a-zA-Z ]*$/
that ensures that at least one character is not whitespace and is of one of the allowed characters.
这确保至少一个字符不是空白字符并且是允许的字符之一。
You may also want to consider other characters like hyphen(-) or apostrophe(') that may also appear in names...
您可能还想考虑其他字符,如连字符(-) 或撇号(') 也可能出现在名称中...
/^\s*[0-9a-zA-Z][0-9a-zA-Z '-]*$/

