Results 1 to 8 of 8

Thread: Search a textbox for a certain character

  1. #1
    Join Date
    Feb 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Search a textbox for a certain character

    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?

    Thanks!

    - I

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

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

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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...

    Code:
    <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

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    <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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Feb 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks again John - I owe you a beer!

  6. #6
    Join Date
    Feb 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    John (or anyone)...

    From this:
    Code:
    <input type="text" onblur="if (this.value.indexOf('\x22')!==-1){alert('Please, use no double quotes!');}">
    specifically this part...:
    Code:
    (this.value.indexOf('\x22')!==-1)
    ...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

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The real javascript fragment concerned here is:
    Code:
    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:
    Code:
    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!')
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Feb 2005
    Posts
    54
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks yet again! That worked.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •