RegEx on Javascript; Checking for instances of one of multiple strings

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

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>

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!

回答1:

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/



回答2:

You can use the code below to check for validation not null, not blank, not undefined and not zero in javascript and jquery.

 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);     }      }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!