Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

Prevent certain words from being typed in a form

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/
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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

Open in new window

Avatar of Aleks

ASKER

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

Do you have a link to your page OR can you post the full HTML you are using?
Avatar of Aleks

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

This is great!  Thank you.