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.
You could do this:
Here
matchand the regular expression/[^\w ]/gis 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 customuniquemethod (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
uniquemethod to remove duplicates, you can use this method: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().