RegEx Javascript;检查多个字符串之一的实例 [英] RegEx on Javascript; Checking for instances of one of multiple strings

查看:68
本文介绍了RegEx Javascript;检查多个字符串之一的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多服务器端表单验证,但是直到现在,我唯一做过的客户端表单验证是检查null/blank条目(例如,if(value ==''|| value == null )).我现在正在检查用户输入的粗俗,并发现使用javascript成功检查了这些(合并到一个变量-vulgarcheck中):

I have done a lot of server side form validation but until now, the only client side form validation I have done is to check for null/blank entries (e.g., if (value==''||value==null) ). I am now checking for user-entered vulgarities and have found success checking for these (incorporated into one variable - vulgarcheck) using javascript as follows:

<script language="javascript" type="text/javascript">
function CheckUserForm() {
var usersuggestion=document.forms['UserForm']['userinput'].value;
var vulgarcheck = /badword1|badword2|badword3|etc/gi;
var vulgarcompare = usersuggestion.match(vulgarcheck);

if (usersuggestion==''||usersuggestion==null) {
alert('To make a suggestion, please enter text into the textbox');
return false;
}
else if (vulgarcompare!=null) {
alert('The text you entered contains some vulgar language. Please try again!');
return false;
}
else {
return true;
}
}
</script>

由于我不熟悉javascript表单验证,因此比检查null/blank-entry更为复杂,因此我希望有人可以告诉我我的方法是否有我不知道的疏漏.也就是说,我缺少这种方法的潜在问题吗?感谢您的帮助!

Since I am new to javascript form validation more complex than a check for a null/blank-entry, I was hoping someone could tell me if my method has any oversight that I am not aware of. That is, are there potential problems with this method that I am missing? Thanks for your help!

推荐答案

我认为,如果您想添加越来越多的单词,在不久的将来很难用低俗的单词来维护字典.

I think that maintaining you dictionary with vulgar words would be hard in near future, when you would like add more and more words.

我的建议是建立字典并使用字典查找. John Resig撰写了两篇有关此事的好文章: http://ejohn.org/blog/javascript-trie-performance-analysis/ http://ejohn.org/blog/dictionary-lookups-in-javascript/

My proposal is to build up an dictionary and use dictionary lookup. John Resig wrote two good articles about this things: http://ejohn.org/blog/javascript-trie-performance-analysis/ and http://ejohn.org/blog/dictionary-lookups-in-javascript/

这篇关于RegEx Javascript;检查多个字符串之一的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆