-1

So I am trying to create a little form validation on entering a Name I want to not allow certain characters, already created names, and internally reserved words. The problem is say when I want to put "posts" in a name it says that it is matching "pos" and fails.

var badchar = new Array('"', "," , "'" , "#", "&", "!", "@", "$", "+", ";", ":", "*", "(",")", "[","]","{","}", "/", "=" );
    var resword = new Array("positive","pos","negative","neg","neutral", "neu","twitter","itunes","facebook","android","forums","RSS Feeds");

    var existingNames = @anames;
    var valueLen = $("#aname").val().length;
    var fail=false;

    var filterElem = $('#aname');
    var filterName = $('#aname').val();

    $.each(badchar, function(char){
        if ( filterName.indexOf(badchar[char]) != -1 ) {
            console.log("bad character")
            filterElem.css('border','2px solid red');
            window.alert("You can not include '" + badchar[char] + "' in your Filter Name");
            fail = true;
        }
    });

    $.each(resword,function(){
        if ( filterName.match(this)) {
                console.log("bad word");
                filterElem.css('border','2px solid red');
                window.alert("You can not include '" + this + "' in your Filter Name");
                fail = true;
            }
    });
BillPull
  • 6,853
  • 15
  • 60
  • 99

4 Answers4

4

Try swapping this line -

if ( filterName.match(this)) {

for this -

var re = new RegExp("\\b" + this + "\\b","g");
if ( filterName.match(re)) {

This should build up a dynamic regex based on your bad word and only match the word if it is a whole word.

ipr101
  • 24,096
  • 8
  • 59
  • 61
1

swap

if ( filterName.indexOf(badchar[char]) != -1 )

for

if ( filterName.match("\\b"+badchar[char]+"\\b") )

\b is regex for matching word boundary

Billy Moon
  • 57,113
  • 24
  • 136
  • 237
0

With the way this question is asked in the header, I would have expected a slightly different question. I found nothing on what I was looking for and have thus solved it for the question asked in the header so that anyone else who runs into this question finds my answer.

This will search and find whole words only. It should never partially match. It will return 3 groups. The previous character, the word, and the character after. Sometimes the character before and after will be nothing.

JS Fiddle Example: http://jsfiddle.net/g8ns3/

var e = new RegExp('(\b|^|[^A-Za-z])(Word)(\b|$|[^A-Za-z])', 'gi');
var text1 = 'Example word. Even when word, is next to a symbol.Word';
var text2 = 'Word Example';
var text3 = 'Example Word';
var text4 = 'word';

if ( text1.match(e) )
    console.log('Found Match Inside 1');

if ( text2.match(e) )
    console.log('Found Match Inside 2');

if ( text3.match(e) )
    console.log('Found Match Inside 3');

if ( text4.match(e) )
    console.log('Found Match Inside 4');

console.log(text1.replace(e, '($1)($2)($3)'));
console.log(text2.replace(e, '($1)($2)($3)'));
console.log(text3.replace(e, '($1)($2)($3)'));
console.log(text4.replace(e, '($1)($2)($3)'));

It works fairly well for detecting or replacing in most cases. If replacing HTML+Text, it may have unexpected results like replacing tags or tag attributes if you have tag or tag attributes with the desired word.

0

Based on your code that is how you have it designed. What are you trying to accomplish? Do you want to disallow the words in the reserved word list only when they are by them self or if they exist in any part of the entered content? If it is former then you don't want to use indexOf since that will match on unintended items (ie "posts" will match "pos") . If you want to not allow any of the reserved words at all in the entered content, then you will have to rethink your design. Not allowing "pos" in any part of the entered content is pretty strict.

Tim Banks
  • 7,099
  • 5
  • 31
  • 28