We help IT Professionals succeed at work.

Prevent certain words from being typed in a form

254 Views
Last Modified: 2017-04-15
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

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

CERTIFIED EXPERT
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?

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

CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
This is great!  Thank you.