locked
detect word RRS feed

  • Question

  • User-81382370 posted

     I need to detect if a textbox contains a certain word, and if it does the form is not valid, Can that be done using a regularexpression validator?

    Friday, August 28, 2009 6:32 AM

Answers

  • User-1696077569 posted

     OK try this.

    Create a property in codebehind to set the word you want to check as

    public string Word
        {
            get;
    
            set;
            
        }


     

    In Page load set property to word as

    Word ="wordtocheck"

    Now add below javascript and custom validator in aspx

     function checkWord(sender, args){
                var word = '<%= Word %>';
                var text = document.getElementById("TextBox1").value;
                
                if (text.indexOf(word) != -1) {
                    args.IsValid = false;
                    alert("1");
                }
                else
                    args.IsValid = true;
                return;
            }
    
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
               <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="checkWord" ControlToValidate="TextBox1" ErrorMessage="CustomValidator" ></asp:CustomValidator>


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2009 10:35 AM

All replies

  • User-1460196090 posted

    Hi,

    You can check this on server side by using: String.Contains method.

    For example:

    if (TextBox1.Text.ToLower().Contains("the_word"))
    {

    //return true

    }

    else //return false

    Hope this helps,
    Hajan

    Friday, August 28, 2009 8:22 AM
  • User-1460196090 posted

    In addition, here is one good example on how to do this using Regular Expressions:

    http://forums.asp.net/t/1403705.aspx#3048395

    Best Regards,
    Hajan

    Friday, August 28, 2009 8:32 AM
  • User-1696077569 posted

     This can be done via a custom validator on client side or server side.

    Friday, August 28, 2009 8:40 AM
  • User-81382370 posted

     Sure this could be don easily on the server side but I don't want to do that, I want to use a regularexpressionvalidator or a customvalidator.

    And the problem is that the usual way is to check for how a string is formated, but in my case I want to check that one particular word is not in the textbox. If it's in the textbox the user should not be allowed to continue

    Friday, August 28, 2009 9:05 AM
  • User-359233574 posted

     what the word you want to validate in textbox, is that word comming for database and is it a dynabmic word you want to test then you need to write a program at backend

    If its a hardcoded word then you can use javascript which is a better option. please let us know so that we can proceed with solution to your problem

    Friday, August 28, 2009 10:04 AM
  • User-81382370 posted

     lets say that the word is "nowayjose", if the user enter this word in a multiline textbox and then press submit, then I want a regular expression validator to prevent the form being submited and popup a alert telling him that this word is not allowed

    Friday, August 28, 2009 10:24 AM
  • User-1696077569 posted

     OK try this.

    Create a property in codebehind to set the word you want to check as

    public string Word
        {
            get;
    
            set;
            
        }


     

    In Page load set property to word as

    Word ="wordtocheck"

    Now add below javascript and custom validator in aspx

     function checkWord(sender, args){
                var word = '<%= Word %>';
                var text = document.getElementById("TextBox1").value;
                
                if (text.indexOf(word) != -1) {
                    args.IsValid = false;
                    alert("1");
                }
                else
                    args.IsValid = true;
                return;
            }
    
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
               <asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="checkWord" ControlToValidate="TextBox1" ErrorMessage="CustomValidator" ></asp:CustomValidator>


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 28, 2009 10:35 AM
  • User-81382370 posted

     Hi Shakti

     

    That worked excellent, Thanks a lot!

    Friday, August 28, 2009 11:12 AM