Prevent certain words from being typed in a form

Aleks
Aleks used Ask the Experts™
on
I am using the following validation script:  http://formvalidation.io/validators/

I can't seem to find a validator to prevent certain words from being entered.  Am I missing something ?
Is there a small script to validate words like "execute", "run", etc from being entered ?

Maybe this can be done with regexp ?

http://formvalidation.io/validators/regexp/
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Year 2008
Top Expert 2008

Commented:
Try item #4 - the "callback" option:
                            callback: function (value, validator, $field) {
                                return ! /\b(execute|run)\b/ig.test(value);
                            }

Open in new window

Author

Commented:
I tried the code below. I don't get a syntax error, but when I type 'run' in the txtusername field I don't get an alert

Do I just add | for every word I want to prevent the user from typing ?

<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(document).ready(function() {

    $('#form1').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            txtusername: {
                validators: {
                    notEmpty: {
                        message: 'The username is required'
                    },
					
					callback: {
                        message: 'The username cannot contain some words',
                        txtusername: {
                    validators: {
                         callback: function (value, validator, $field) {
                                return ! /\b(execute|run|truncate|shutdown)\b/ig.test(value);
                            }
                     }
 					}
					 }
					 }
            },
			
			txtpassword: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    },
                    
                }
            },
					
  }
		
    });
});
});//]]>  

</script>

Open in new window

Expert of the Year 2008
Top Expert 2008

Commented:
Do you have a link to your page OR can you post the full HTML you are using?
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
It is an intranet, but here is the relevant code. The form and the validation. The rest of the validation works fine, it doesn't allow the txtusername field to be empty.

          <form action="Index.asp" method="post" name="form1" id="form1" >
               <input name="frmAction" type="hidden" form="form1" value="Submit">
               <div class="form-group">
                    <input name="txtusername" type="text" required class="form-control" id="txtusername" placeholder="username" tabindex="1" title="Please enter a value." value="" maxlength="100">
                  
                </div>
                <div class="form-group">
                    <input name="txtpassword" type="password" required class="form-control" id="txtpassword" placeholder="password" tabindex="2" title="Please enter a value."  value=""  maxlength="25" confirm="">
                </div>
                <input class="btn btn-primary block full-width m-b" name="LogIn_submit" type="submit" id="LogIn_submit" value="Sign In"   tabindex="5">
             
          </form>


AND THIS IS THE VALIDATION CODE:


<script type='text/javascript'>//<![CDATA[ 
$(window).load(function(){
$(document).ready(function() {

    $('#form1').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            txtusername: {
                validators: {
                    notEmpty: {
                        message: 'The username is required'
                    },
					
					callback: {
                        message: 'The username cannot contain some words',
                   
                    validators: {
                         callback: function (value, validator, $field) {
                                return ! /\b(execute|run|truncate|shutdown)\b/ig.test(value);
                 
                     	}
 					}
				}
			}
           },
			
			txtpassword: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    },
                    
                }
            },
					
  }
		
    });
});
});//]]>  

</script>

Open in new window

Expert of the Year 2008
Top Expert 2008
Commented:
You defined it as:
"validators":{"callback":{"message":"...", "validators":{ "callback": function(){...} } }}

That inner validators should not be there.

Try:
<script type="text/javascript">
$(document).ready(function() {

    $('#form1').formValidation({
        framework: 'bootstrap',
        icon: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            txtusername: {
                validators: {
                    notEmpty: {
                        message: 'The username is required'
                    },

                    callback: {
                        message: 'The username cannot contain some words',
                        callback: function(value, validator, $field) {
                            return !/\b(execute|run|truncate|shutdown)\b/ig.test(value);

                        }
                    }
                }
            },

            txtpassword: {
                validators: {
                    notEmpty: {
                        message: 'The password is required'
                    },

                }
            },

        }

    });

});
</script>

Open in new window

Author

Commented:
This is great!  Thank you.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial