View Full Version : Search a textbox for a certain character

07-18-2005, 08:30 PM
Howdy folks,

I'm searching for a Javascript function that will search the contents of a textbox for a certain [named] character or characters that may have been entered and will alert the user (when they leave that textbox) to revise their comments to exclude these characters. For example, if a user types in comments into a textbox such as "Hello!, my name is Mud" and then leaves the textbox, and if I did not want exclamation points listed (for whatever reason), I would like to force an alert to the user to revise their comments omitting the exclamation point before proceeding.

I toyed around with one of the character limit Javascripts (that checks for greater than x characters and forces an alert), but can't seem to uncover the logic for checking contents of the textbox as opposed to size/length.

Does anyone have a similar script or know how I can achieve this?


- I

07-19-2005, 01:26 AM
<input type="text" onblur="if (this.value.indexOf('!')!==-1){alert('Please, no exclamation points!');this.focus()}">

07-19-2005, 03:49 PM
Thanks John,

That works - unfortunately, I goofed in my example - I need to check for double-quotes (and not exclamation points). It dosn't appear as if I can simply replace the ! with a " in this string...

<input type="text" onblur="if (this.value.indexOf('!')!==-1){alert('Please, no exclamation points!');this.focus()}">

Background: I am extracting data from this form using a SQL task which exports the data into a txt file. This is using comma delimeter with double quotes around the text string as users are prone to inputting commas in their comments. I need to make sure they do not use double quotes in their comments. So similar to what you depicted with the exclamation point, I need to do the same but looking for double quotes... would you (or anyone) happen to know the syntax?

Thanks again.

- Isaac

07-19-2005, 04:59 PM
<input type="text" onblur="if (this.value.indexOf('\x22')!==-1){alert('Please, use no double quotes!');}">Note:
I took away the 'this.focus' near the end, as it was leading to weird results in Mozilla if the focus shifted away from the window while double quotes where still in the input field, and was not giving focus back to the text input. Something else you might want to consider is not bothering your users at all with an alert box. If it won't cause other problems, change the double quotes (if any) to single ones before handing the string over to the next step.

07-19-2005, 06:09 PM
Thanks again John - I owe you a beer! :)

08-23-2005, 07:02 PM
John (or anyone)...

From this:

<input type="text" onblur="if (this.value.indexOf('\x22')!==-1){alert('Please, use no double quotes!');}">

specifically this part...:


...if I wanted to block out more than one character (let's say the letters a and/or x) how would I modify the above snippet?

So if a user types in a letter a (example), it would force the alert message - or if the user types in a letter x, it would force the alert message... is that possible, or do I have to list the onblur string twice - each with it's own parameter?

Thanks again,

- I

08-23-2005, 07:18 PM
The real javascript fragment concerned here is:

if (this.value.indexOf('\x22')!==-1)Now, to include other characters we can expand it. If there are numerous other characters, we should create and reference a script in the head of the document but, for just two more, it should not be too unwieldy:

if (this.value.indexOf('\x22')!==-1||this.value.indexOf('a')!==-1||this.value.indexOf('x')!==-1))But, you will want to change the alert message too:

alert('Please, use no double quotes, a\'s or x\'s!')

08-23-2005, 07:23 PM
Thanks yet again! That worked.