Solved

Javascript Disable URL Form

Posted on 2007-04-10
7
797 Views
Last Modified: 2008-02-01
I would like to disable people from inserting links into my form. Are there any tutorials out there for this?
0
Comment
Question by:Nitestarz
  • 3
7 Comments
 
LVL 7

Accepted Solution

by:
mzalfres earned 25 total points
ID: 18881813
Hi,

I'd search for regular expression defining any/specific URL and then create script running with 'onSubmit' event, which will check the form against this.

Something like in this example:

http://snippets.dzone.com/posts/show/452

If 'is_URL' function returns true, issue alert/error/whatever else and stop submitting form.

Regards,

Marek ZJ.
0
 
LVL 19

Expert Comment

by:v2Media
ID: 18882023
Javascript will not do it for you; it's too easily disabled. If you want to forbid links in your form data, then you have to do it server-side. Does your server support asp or php or any other server-scripting language?
0
 

Author Comment

by:Nitestarz
ID: 18882085
Yes, PHP is used to process the page.
0
 
LVL 19

Assisted Solution

by:v2Media
v2Media earned 25 total points
ID: 18882570
ok, lets say you want to show an error message to alert the user that links are not permitted. In the form processing portion of your script, include something along these lines:-

Assume $_POST['Submit'] is your form's submit button.

<?php
if(isset($_POST['Submit'])) {

      // reset onpage errors
      $onpageErrors = ''
      
      // concatenate POST vars to $strHaystack
      $strHaystack = '';
      foreach ($_POST as $index => $value){
      
            $strHaystack .= $value;
            
            }
      
      //detect link spammer
      $no_links = false;      
      $needles = '';
      
      //link needles
      $needles[] = '<a href';
      $needles[] = '=http:';
      $needles[] = '="http:';
      $needles[] = '=mailto:';
      
      foreach ($needles as $k => $v){
      
                  if( stristr($strHaystack,$v ) ){
                  $onpageErrors .= "Do not include links in this contact form. Use the format &quot;www.domain.com"; only.<br />";
                  break;
                  
                  }
            }
            
      if($onpageErrors == '') {
            //continue with form processing as usual
      
      }

}//end if form is submitted
?>

0
 
LVL 19

Expert Comment

by:v2Media
ID: 18882610
This is a snippet from a larger anti-spam script I use regularly. the line " $no_links = false;" can be deleted. Echo $onpageErrors someplace near your form if it doesn't = ''

e.g.

<?php
if($onpageErrors != '') {
?>
<div><?php echo $onpageErrors; ?></div>
<?php } ?>
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question