简体   繁体   English

RegEx Javascript; 检查多个字符串之一的实例

[英]RegEx on Javascript; Checking for instances of one of multiple strings

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

<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>

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. 由于我不熟悉javascript表单验证,因此比检查null / blank-entry更为复杂,因此我希望有人可以告诉我我的方法是否有我不知道的疏忽。 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. 我认为,在不久的将来,当您想添加越来越多的单词时,很难用低俗的单词来维护字典。

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/ John Resig撰写了两篇有关此事的好文章: http : //ejohn.org/blog/javascript-trie-performance-analysis/http://ejohn.org/blog/dictionary-lookups-in-javascript/

You can use the code below to check for validation not null, not blank, not undefined and not zero in javascript and jquery. 您可以使用下面的代码来检查验证,这些验证不是javascript,jquery中的非null,非空白,undefined以及非零。

 function myFunction() {
    var data;  //The Values can be like as null,blank,undefined,zero you can test

     if(!(!(data)))
     {
     alert("data "+data);
     } 
     else 
     {
        alert("data is "+data);
    }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM