Avatar of Aleks
Aleks
Flag 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/
Web DevelopmentASPJavaScriptjQueryBootstrap

Avatar of undefined
Last Comment
Aleks

8/22/2022 - Mon
hielo

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

Open in new window

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

hielo

Do you have a link to your page OR can you post the full HTML you are using?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
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
hielo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Aleks

ASKER
This is great!  Thank you.