Hi All,

I would like to know what's the best way to write function(global) in asp.net c# Framework 2.0 to check for textbox in a form (i have about 80 textbox on one form and I have many forms and textarea
and I have list of 90 ro 100 words and special chracter that I do not want user to enter. If they enter the item from the list of blocked word or character I want to give them error saying "this character or word in not allowed, please correct it"
The site is getting many hits already so I want to make sure it's not going to slow down a lot. I want to give the message as soon as one of the word or special character listed is found and not want to continue checking further.

e.g of block list word "abc","bbc","bbd","cbc","xbc","~","^","'". the list can change in the future.

I am new to asp.net and appreciate any help.

Thanks
newcoder

Dani AI

Generated

Given the scale you described (about 80 fields on a page, ~90–100 blocked tokens, and future changes), a single, fast server-side validator class is the right place to centralize checks for both textboxes and querystring values. wants immediate short-circuiting when a match is found; already pointed toward validation, so the goal here is a high-performance, maintainable implementation that stops at the first hit and can be re-used across pages.

A practical, low-complexity implementation uses two structures: a tiny lookup for single blocked characters and a list/trie for blocked words. Scanning characters with a fixed-size boolean array is O(n) and very cheap; for words you can try a simple IndexOf loop (early exit on first found) and upgrade to a multi-pattern automaton (Aho–Corasick) when throughput demands it.

using System;
using System.Collections.Generic;

public static class InputValidator
{
    static bool[] blockedAscii = new bool[128];        // set true for blocked chars
    static List<string> blockedWords = new List<string>(); // load from file/db once

    public static bool ContainsBlocked(string text, out string matched)
    {
        matched = null;
        if (string.IsNullOrEmpty(text)) return false;

        foreach (char c in text) {
            if (c < 128 && blockedAscii[c]) { matched = c.ToString(); return true; }
        }

        foreach (string w in blockedWords) {
            if (text.IndexOf(w, StringComparison.OrdinalIgnoreCase) >= 0) { matched = w; return true; }
        }
        return false;
    }
}

Load the blocklist once at app start (or cache it in memory) and reload on change (FileSystemWatcher) so updates don’t require recompiles. For large pattern sets and heavy traffic, replace the word loop with an Aho–Corasick implementation to scan input in one pass (). Use server-side validation as authoritative (never rely only on client-side), normalize inputs (trim, case/Unicode), limit lengths, and always use parameterized queries or encoding when those values hit a database or HTML output.

Recommended Answers

All 2 Replies

hi..

you want to restrict some specific words for a specific textbox? for ex, is it like you wanna restrict "abc","pqr","Xxx" for textbox1 & "^","&","#" for textbox10? or the words/chars are fixed for each n every textbox on the form??

you can either go for Regular expression or use client side validator for each text box...

for regular Expression click Here, Here &

Thanks I am using regular expression for now but I would like to create function so I can use it for querystring parameters also (just in case)and I want to store that in a class and validate with just passing through the function.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.