SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alert to check for a text box containing <a href=

    I have very little javascript knowledge so would appreaciate some help onthis please.

    I have the following code on my form test.

    Code:
    		  if (formx.description.value == "") {
            	alert("Please enter a description of what you are contacting David Brown about.");
    			formx.description.focus()
            	return false;
            }
    But I also want to be able to test to see if the message field contains the following

    <a href=
    </a>

    If it does I would like an error message that comes up saying you can't include links. Is this possible?

  2. #2
    ********* Staff mattevans's Avatar
    Join Date
    Sep 2009
    Location
    Melbourne, Australia
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Struggling,

    Try something like...

    Code JavaScript:
    dirtyValue = document.formx.description.value;
    cleanValue = dirtyValue.replace(/<a\b[^>]*>(.*?)<\/a>/i,"");

    ... this will remove <a>'s from the designated value.
    Matt Evans
    Sitepoint Developer
    tweet 2 me

  3. #3
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will this stop the form being submitted?

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    This strip_tags code might be very useful for you:
    JavaScript strip_tags

    That way you can automatically remove all tags from the form value:

    Code javascript:
    formx.description.value = striptags(formx.description.value);

    Or you can choose to remove only certain tags that you care about, such as links and scripts:

    Code javascript:
    formx.description.value = striptags(formx.description.value, '<a><script>');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •