Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1029881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:36:54+00:00 2026-05-16T12:36:54+00:00

There is a textbox and a button that validates the entry inside the textbox.

  • 0

There is a textbox and a button that validates the entry inside the textbox.
That if it should not validate the presence of illegal characters in the form.
Suppose I entered the following word “Lorem# %ipsum^”

On clicking the button, two things should happen

  • If there sre any special chars like #$%^&, then the form submission should fail.
  • An error message should pop up like “You have used the illegal characters #, % and ^ in your form”

What is the best method?
P.S: I would like a solution not involving jquery.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:36:55+00:00Added an answer on May 16, 2026 at 12:36 pm

    You could do this:

    var invalidChars = str.match(/[^\w ]/g), output = '';
    if (invalidChars) {
        invalidChars = invalidChars.unique();
        var lastInvalidChar = invalidChars.pop();
        output = 'You have used the illegal character' + (invalidChars.length > 0 ? 's' : '') + ' ' +
            invalidChars.join(', ') + (invalidChars.length > 0 ? ' and ' : '') + lastInvalidChar;
    }
    

    Here match and the regular expression /[^\w ]/g is used to get all characters that are neither word characters nor a space. The array of matched invalid characters is then cleaned from duplicates using the custom unique method (see below). Then the last invalid character is removed from the array to append it if necessary with an “and” at the end of the output. The remaining invalid characters (if there are any) are then joined with commas and combined with the last invalid character.

    Since JavaScript has no built-in unique method to remove duplicates, you can use this method:

    Array.prototype.unique = function() {
        var index = {};
        for (var i=0; i<this.length; ++i) {
            if (!index.hasOwnProperty(this[i])) {
                index[this[i]] = this[i];
            }
        }
        var clean = [];
        for (var prop in index) {
            if (index.hasOwnProperty(prop)) {
                clean.push(index[prop]);
            }
        }
        return clean;
    };
    

    Note that this implementation is not type-safe as the values are used as property names and thus turned into strings. So ["3",3].unique() returns ["3"] hence "3".toString() === (3).toString().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my form there is a textbox and submit button. When a user starts
There are a button and a textbox. I added a KeyDown event to textbox
In below link there is add button which allows you to ADD multiple textbox
Is there a way to hide the textbox that is targeted by the colorpickerextender
Is there any way to get a TextBox in j2me not editable? Because I
Hi I want to validate a form using a button, but this button call
I have a button that will create a new entry (row in a table)
one of the fields on my form is a textbox that the user types
I have the following problem in ASP.NET: there is a form that contains a
Using jquery to validate that there is text in a text box. When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.